Self-written MySQL pagination show.

Source: Internet
Author: User
Tags rowcount

relatively simple. The thought of the teacher of Chuan Zhi Han, can be directly to use Kazakhstan. Layered thinking. You haven't studied MVC yet, just record it here.

First of all. Encapsulates the information needed for paging into a class, ApartPage.class.php

<?PHPclassapartpage{ Public $rowCount; How many lines are there in total? Public $pageCount; How many pages are there in total Public $pageNow=1; Current Page Public $pageSize=5; How many rows are displayed per page Public $res _array;        The equivalent array of result sets retrieved after select.  Public $page _whole=10; How many pages are there in the navigation bar, 1 2 3 4 5 6 7 8 9 Public $navigator;    The navigation bar string below the page. }    ?>

The lowest-level function for database operations.

<?PHP Public functionExecute_dql_apartpage ($sql 1,$sql 2,$apartPage){                 $res=$this->conn->query ($sql 1) or die($this-&GT;CONN-&GT;ERROR);//returns the result set performed by the Select COUNT (ID)                if($row=$res-Fetch_row ()) {                 $apartPage->rowcount=$row[0];//using SQL1 to remove rowcount}$res-free ();//release result set//Var_dump ($sql 2); Die ();                 $res 2=$this->conn->query ($sql 2) or die($this-&GT;CONN-&GT;ERROR);//Returns the result set obtained after executing the query;                 $arr=Array();  while($row=$res 2-Fetch_row ()) {                     $arr[]=$row;//Use SQL2 to take out all the rows and contents, and assign them to an array to easily release resources directly. }
$res 2->free ();//release result set$apartPage->res_array=$arr; $apartPage->pagecount=Ceil($apartPage->rowcount/$apartPage-pageSize); //The following shows the navigation bar area. if($apartPage->pagenow>1){ $prePage=$apartPage->pagenow-1; $apartPage->navigator= "<a href= ' emplist.php?pagenow=$prePage' > Prev </a> '; } if($apartPage->pagenow<$apartPage-PageCount) { $nextPage=$apartPage->pagenow+1; $apartPage->navigator.= "<a href= ' emplist.php?pagenow=$nextPage' > Next </a> '; } $start= Floor(($apartPage-&GT;PAGENOW-1)/$apartPage->page_whole) *$apartPage->page_whole+1;//This number is the value of the first number in the following hyperlink number arrangement. $index=$start;// for(;$start<$index+$apartPage->page_whole;$start++){ $apartPage->navigator.= "<a href= ' emplist.php?pagenow={$start} ' >[$start]&nbsp</a> "; //If start grows to the same number of pages, it jumps out of the loop. if($start==$apartPage-PageCount) { Break; } } if($apartPage->pagenow<$apartPage-PageCount) { $apartPage->navigator.= "&nbsp&nbsp<a href= ' emplist.php?pagenow=$start' >>></a> ';//Turn to the next Page_whloe page}//$page _whloe=;//The number of numbers shown in the column below. if($apartPage->pagenow>$apartPage-page_whole) { $preRow=( Floor($apartPage->pagenow/$apartPage->page_whole)-1) *$apartPage->page_whole+1;//here is OK $apartPage->navigator.= "&nbsp&nbsp<a href= ' emplist.php?pagenow=$preRow' &GT;&LT;&LT;&LT;/A&GT;&AMP;NBSP&AMP;NBSP ';//Turn to the previous Page_whole page. } //return $apartPage;}?>

In the previous layer of database operations, encapsulate a service class and invoke the underlying Execute_dql_apartpage function. EmpService.class.php

<?PHP Public functionGetapartpage ($apartPage){                $sqliHelper=NewSqlihelper (); $sql 1= "SELECT count (ID) from myemp"; $rowBegin=$apartPage->pagesize* ($apartPage->pagenow-1); $sql 2= "SELECT * from Myemp limit {$rowBegin},{$apartPage->pagesize} "; $sqliHelper->execute_dql_apartpage ($sql 1,$sql 2,$apartPage); }?>

After the function call above, all the information needed for paging is saved $apartPage object.

Specific invocation.

<?php
Require_once' EmpService.class.php ';require_once' ApartPage.class.php ';$apartPage=NewApartpage (); $empService=NewEmpservice (); //assigns a value to Pagenow before the Getapartpage function executes. if(isset($_get[' Pagenow '])){ $apartPage->pagenow=$_get[' Pagenow ']; } $empService->getapartpage ($apartPage); for($i= 0;$i<$apartPage->pagesize;$i++) { Echo"<tr>"; $row[Email protected]$apartPage->res_array[$i]; if(isset($row)){//determines if $row is empty and does not empty to make the following statement foreach($row as $k=$val){ Echo"<td>$val</td> "; } Echo"</tr>"; } } Echo"</table>"; //the inability to use self-increment can cause pagenow data to change. It can be achieved with prepage and nextpage instead. Echo $apartPage-navigator;//here Show navigation bar previous page next page 1 2 3 4 5 << >> et?>

At this point, the hierarchical mode of paging is completed, the bottom of the execute_dql_apartpage, for all the database paging operation, the required parameters are a query database has how many records of the statement, a query to display the data of the statement.

This content is written on specific pages to serve

Self-written MySQL pagination show.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.