php分页思路以及在ZF中的使用_php实例
只需要得到两个变量就成功了一半:
每页要显示的记录数$pageSize
表中总的数据量 $rowCount
有了以上两个变量,我们就可以得出 共有几页了$pageCount
然后通过for循环,比如总共有13个页面,那么很容易就能通过for循环输出页数
以上的for循环将输出如
第1页,第2页,第3页,第4页,第5页,第6页,第7页,第8页,第9页,第10页,第11页,第12页,第13页
如果我们只想每次只显示十个页面呢?比如1-10页,11-20页
很简单,只要稍微修改下for循环即可实现
比如,当前页面$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开发_小飞

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

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 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 is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

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.

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 suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
