Home > Backend Development > PHP Tutorial > The idea of ​​paging and its use in ZF_PHP tutorial

The idea of ​​paging and its use in ZF_PHP tutorial

WBOY
Release: 2016-07-13 17:51:49
Original
979 people have browsed it

Paging ideas:
Only needing to get two variables is half the battle:
1. The number of records to be displayed on each page $pageSize
2. The total amount of data in the table $rowCount
With the above two variables, we can figure out how many pages there are $pageCount
Then through a for loop, for example, if there are 13 pages in total, it is easy to output the number of pages through the for loop
$nav='';//A variable used to save the page number
for ($i=1;$i<=13;$i++)
{
​$nav.="Page ".$i." ";
}
The above for loop will output something like
Page 1, Page 2, Page 3, Page 4, Page 5, Page 6, Page 7, Page 8, Page 9, Page 10, Page 11, Page 12, Page 13 Page
What if we only want to display ten pages at a time? For example, pages 1-10, pages 11-20
It's very simple, just slightly modify the for loop to achieve it
$step= floor(($pageNow-1)/10)*10+1;
for ($i=$step;$i<=$step+10;$i++)
{
​$nav.="Page ".$i." ";
}
For example, if the current page $pageNow is between 1 and 10, then $step=0
If the current page $pageNow is between 11~20, then $step=10
If the current page $pageNow is between 21~30, then $step=20
Referring to the code of the specific implementation process, it is not difficult to find that the second condition of the for loop only needs to add 10 to achieve the situation of only displaying 10 each time. We will package this step in the getLink in the fenyePage class. () method
Then again, how can we get the values ​​of the $pageSize and $rowCount variables?
$pageSize can be specified by the programmer himself, and $rowCount can be obtained with the help of a simple function that executes sql statements


1 2 /**
3 * $sql statement: ① Get data ② Get total number of records
4*/
5 class fenyePage{
6​​ public $pageSize=5;//The number displayed on each page-->Specified by the programmer
7​​ public $rowCount;//This is obtained from the database (in the form of SELECT COUNT(id) FROM TABLE) and is used to save the total number of records
8​​ public $pageNow;//obtained through $_GET['page'], used to save the current page number
9​​ public $pageCount;//Calculated, used to save the total number of pages
10 Public $res_arr;//Used to save the data to be displayed on the page (such as saving the data retrieved by SELECT * FROM TABLE LIMIT 0,10)
11 Public $nav;//Display the navigation bar of which page
12
13 /**
14 * Get the hyperlink of the current page
15 *
16 * @author Xiaofei 2012/5/30
17*/
18 public function getLink()
19 {
20          $this->nav='';
21            $this->pageCount=ceil(($this->rowCount/$this->pageSize));
22          $step= floor(($this->pageNow-1)/10)*10+1;
23 if ($this->pageNow>10)
24 {
" 10 pages forward
26 }
27 if ($this->pageNow!=1)
28 {
29                $this->nav.=" Previous page ";
30 }
31 if ($this->pageNow!=1)
32             {
33                      $this->nav.="Homepage ";
34 }
35 for ($start=$step;$start<$step+10 && $start<=$this->pageCount;$start++)
36 {
37             $this->nav.="".$start."  ";
38         }
39         if ($this->pageNow!=$this->pageCount)
40         {
41             $this->nav.="末页   ";
42         }
43         if ($this->pageNow!=$this->pageCount)
44         {
45             $this->nav.="   下一页";
46         }
47         if ($this->pageCount>10 && $this->pageNow<$this->pageCount-8){
48             $this->nav.="   >> ";//整体每10页向后翻
49         }
50         $this->nav.="/共有".$this->pageCount."页";
51     }
52 }
53 ?>
 
由于zf中操作数据库的任务由model层来完成,所以,我将获取$rowCount的值的函数放在了对应的表model中
比如:我是操作order表的
那么当我要显示所有订单信息的时候,我通过order类中的showorder()方法取得$rowCount的值,并将其付给分页类中的$rowCount属性
同样,将要显示在页面上的数据信息也一并付给了分页类中的$res_arr属性
这样,我们就可以很容易的通过实例化一个分页类(fenyePage),然后将其通过参数传给showorder()函数,由该函数完成以下动作:
①要显示在页面上的信息
②表中总共有多少条记录
 
 1 /**
2 * Based on the specified user ID, query the user’s historical ordering records
3 *
4 * @author Xiaofei 2012/5/30
5 * @param $id user id
6 * @param $fenye An object instantiated to handle paging
7 * @todo $sql1 statement "select * from table where * limit 0,10" This sql statement is mainly used to retrieve data in the database and display it in the view layer
8 * @todo $sql2 statement "select count(id) from table" This sql statement is used to get the total amount of data
9*/
10     public function showorder($id=null,$fenye=null)
11     {
12         $db = $this->getAdapter();
13        
14         $select=$db->select();
15         $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'));
16         if ($id!=null){
17             $select->where('o.user_id=?',$id);   
18         }
19         $select->join(array('d'=>'department'),'o.dep_id = d.id','d.dep_name');
20         if($fenye!=null){
21             $select->limit($fenye->pageSize,($fenye->pageNow-1)*$fenye->pageSize);
22         }
23         $sql1=$select->__toString();
24                  //This sql statement is mainly used to calculate the total amount of data
25           $sql2="SELECT COUNT(id) FROM `order`";
26           $fenye->res_arr=$db->fetchAll($sql1);//Store the data to be displayed in the $res_arr attribute of the paging class for easy calling
27           $rowCount=$db->fetchAll($sql2);//Save the total amount of data in the table to the rowCount attribute of the paging class
28            $fenye->rowCount=$rowCount[0]['COUNT(id)'];
29          $fenye->getLink();
30         return $fenye->res_arr;
31 }

At this point, the paging function has been implemented


Excerpted from WEB Development_Xiao Fei

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478178.htmlTechArticlePaging idea: You only need to get two variables to get half the success: 1. The number of records to be displayed on each page$ pageSize 2. The total amount of data in the table $rowCount With the above two variables, we can get...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template