只需要得到两个变量就成功了一半:
每页要显示的记录数$pageSize
表中总的数据量 $rowCount
有了以上两个变量,我们就可以得出 共有几页了$pageCount
然后通过for循环,比如总共有13个页面,那么很容易就能通过for循环输出页数
复制代码 代码如下:
$nav='';//用来保存页数的一个变量
for ($i=1;$i{
$nav.="第".$i."页 ";
}
以上的for循环将输出如
第1页,第2页,第3页,第4页,第5页,第6页,第7页,第8页,第9页,第10页,第11页,第12页,第13页
如果我们只想每次只显示十个页面呢?比如1-10页,11-20页
很简单,只要稍微修改下for循环即可实现
复制代码 代码如下:
$step= floor(($pageNow-1)/10)*10+1;
for ($i=$step;$i{
$nav.="第".$i."页 ";
}
比如,当前页面$pageNow如何在1~10之间的话,那么$step=0
当前页面$pageNow如何在11~20之间的话,那么$step=10
当前页面$pageNow如何在21~30之间的话,那么$step=20
参考具体的实现过程的代码,我们不难发现,for循环的第二个条件只需要加上10就可以实现每次只显示10也的情况了,我们将这一步分装在fenyePage类中的getLink()方法中
话又说回来,如何才能得到$pageSize和$rowCount两个变量的值呢?
$pageSize可以又程序员自己指定,$rowCount可以借助一个简单的执行sql语句的函数就能得到
复制代码 代码如下:
/**
* $sql语句:①获取数据②获取总记录数
*/
class fenyePage{
public $pageSize=5;//每页显示的数量-->程序员指定的
public $rowCount;//这是从数据库中获取的(形如SELECT COUNT(id) FROM TABLE)用来保存总共有多少条记录
public $pageNow;//通过$_GET['page']获取的,用来保存当前所在的页码
public $pageCount;//计算得到的,用来保存总共有多少页
public $res_arr;//用来保存要显示到页面的数据(比如保存SELECT * FROM TABLE LIMIT 0,10 检索的数据)
public $nav;//显示第几页第几页的导航条
/**
* 取得当前页面的超链接
*
* @author 小飞 2012/5/30
*/
public function getLink()
{
$this->nav='';
$this->pageCount=ceil(($this->rowCount/$this->pageSize));
$step= floor(($this->pageNow-1)/10)*10+1;
if ($this->pageNow>10)
{
$this->nav.=" ";//整体每10页向前翻
}
if ($this->pageNow!=1)
{
$this->nav.=" 上一页 ";
}
if ($this->pageNow!=1)
{
$this->nav.="首页 ";
}
for ($start=$step;$startpageCount;$start++)
{
$this->nav.="".$start." ";
}
if ($this->pageNow!=$this->pageCount)
{
$this->nav.="末页 ";
}
if ($this->pageNow!=$this->pageCount)
{
$this->nav.=" 下一页";
}
if ($this->pageCount>10 && $this->pageNowpageCount-8){
$this->nav.=" >> ";//整体每10页向后翻
}
$this->nav.="/共有".$this->pageCount."页";
}
}
?>
由于zf中操作数据库的任务由model层来完成,所以,我将获取$rowCount的值的函数放在了对应的表model中
比如:我是操作order表的
那么当我要显示所有订单信息的时候,我通过order类中的showorder()方法取得$rowCount的值,并将其付给分页类中的$rowCount属性
同样,将要显示在页面上的数据信息也一并付给了分页类中的$res_arr属性
这样,我们就可以很容易的通过实例化一个分页类(fenyePage),然后将其通过参数传给showorder()函数,由该函数完成以下动作:
①要显示在页面上的信息
②表中总共有多少条记录
复制代码 代码如下:
/**
* 根据指定的用户id,查询该用户的历史订餐记录
*
* @author 小飞 2012/5/30
* @param $id 用户id
* @param $fenye 实例化的一个对象,用来处理分页
* @todo $sql1语句 "select * from table where * limit 0,10" 该sql语句主要用来检索数据库中的数据,用以显示在view层
* @todo $sql2语句 "select count(id) from table" 该sql语句用来得出总的数据量
*/
public function showorder($id=null,$fenye=null)
{
$db = $this->getAdapter();
$select=$db->select();
$select->from(array('o' => 'order'),array('o.id','o.user_id','o.user_name','o.food_name','o.food_price','o.order_time','o.order_state'));
if ($id!=null){
$select->where('o.user_id=?',$id);
}
$select->join(array('d'=>'department'),'o.dep_id = d.id','d.dep_name');
if($fenye!=null){
$select->limit($fenye->pageSize,($fenye->pageNow-1)*$fenye->pageSize);
}
$sql1=$select->__toString();
//该sql语句主要用来计算总的数据量
$sql2="SELECT COUNT(id) FROM `order`";
$fenye->res_arr=$db->fetchAll($sql1);//将要显示的数据存储到分页类的$res_arr属性当中,方便调用
$rowCount=$db->fetchAll($sql2);//将表中的总数据量保存到分页类的rowCount属性当中
$fenye->rowCount=$rowCount[0]['COUNT(id)'];
$fenye->getLink();
return $fenye->res_arr;
}
至此,分页类的功能就已经实现了
原创文章:WEB开发_小飞

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.