The examples in this article describe the simple pagination classes and usage of PHP implementations. Share to everyone for your reference, specific as follows:
<?php * * Use: * $page = new page (connector, query statement, current page number, per page size, page number character) * connector: A MySQL connection identifier, if this argument is left blank, use the most recent connection * query statement: SQL statement * Current page
Code: Specify current Page * per page size: Number of records per page * page character: Specify current page URL format * Use example: * $sql = "SELECT * from AA";
* $page = new page ($conn, $sql, $_get[' page '],4, "? page=");
* * Get current page number * $page->page;
* * Get the total number of pages * $page->pagecount;
* * Total Record number * $page->rowcount;
* * To obtain the record number of this page * $page->listsize;
* Get Recordset * $page->list;
* The Recordset is a 2-D array, example: list[0][' ID ' accesses the ID field value of the first record.
* * Get a list of page numbers * $page->getpagelist ();
*/class Page {//base data var $sql;
var $page;
var $pageSize;
var $pageStr; var $pageCount statistic data; Number of pages Var $rowCount; Record number//result data var $list = array ();
Result row array Var $listSize;
constructors function Page ($conn, $sql _in, $page _in, $pageSize _in, $pageStr _in) {$this->sql = $sql _in;
$this->page = intval ($page _in);
$this->pagesize = $pageSize _in;
$this->pagestr = $pageStr _in; A processing if (! $this->page|) with an empty page number or less than 1 | $this->page<1) {$this->page = 1; The total number of records $ROWCOUNTSQL = Preg_replace ("/" [\w\w]*?select) ([\w\w]*?) (from[\w\w]*?)
/i "," Count (0) $ ", $this->sql);
if (! $conn) $rs = mysql_query ($ROWCOUNTSQL) or Die ("Bnc.page:error on getting rowcount.");
else $rs = mysql_query ($ROWCOUNTSQL, $conn) or Die ("Bnc.page:error on getting rowcount.");
$rowCountRow = Mysql_fetch_row ($RS);
$this->rowcount= $rowCountRow [0]; Calculate Total Pages if ($this->rowcount% $this->pagesize==0) $this->pagecount = intval ($this->rowcount/$this->p
Agesize);
else $this->pagecount = intval ($this->rowcount/$this->pagesize) +1;
SQL Offset $offset = ($this->page-1) * $this->pagesize; if (! $conn) $rs = mysql_query ($this->sql. "Limit $offset,". $this->pagesize) or Die ("Bnc.page:error on listing.
"); else $rs = mysql_query ($this->sql. "Limit $offset,". $this->pagesize, $conn) or Die ("Bnc.page:error on listing. ");
while ($row =mysql_fetch_array ($rs)) {$this->list[]= $row;
$this->listsize = count ($this->list);
The * * * Getpagelist method generates a simpler page number list * If you need to customize the page number list, you can modify the code here, or use the total number of pages/Total records and other information to calculate the build.
* * Function Getpagelist () {$firstPage;
$previousPage;
$pageList;
$nextPage;
$lastPage;
$currentPage; If the page number >1 displays the first page connection if ($this->page>1) {$firstPage = "<a href=\" ". $this->pagestr."
1\ "> Home </a>"; //If the page number >1 displays the previous page connection if ($this->page>1) {$previousPage = "<a href=\". $this->pagestr. ( $this->page-1). "
\ "> Prev </a>"; //If not to the last end then display the next page connection if ($this->page< $this->pagecount) {$nextPage = "<a href=\" ". $this->pag Estr. ($this->page+1). "
\ "> next page </a>"; //If not to the last end, display the last connection if ($this->page< $this->pagecount) {$lastPage = "<a href=\". $this->page Str. $this->pagecount. "
\ "> Last </a>";
} All page number list for ($counter =1 $counter <= $this->pagecount; $counter + +) {if ($this->page = = $counter) {$currentPage = "<b>". $counter. "
</b> "; } else {$currentPage = "". " <a href=\ "". $this->pagestr. $counter. " ">". $counter. " </a> "."
";
$pageList. = $currentPage; return $firstPage. " ". $previousPage." ". $pageList." ". $nextPage." ". $lastPage."
";
}}?>
Usage examples:
<?php
@ $db = mysql_connect (' localhost ', ' root ', ' 123456 ') or
die ("Could not connect to database."); /Connection Database
mysql_query ("Set names ' UTF8");//Output Chinese
mysql_select_db (' test '); Select database
$sql = "SELECT * from ' users '";/a simple query
$page = new Page (", $sql, $_get[' page '],5,"? page= ");
$rows = $page->list;
foreach ($rows as $row)
{
echo $row [' UserName ']. <br> ";
}
echo $page->getpagelist (); Output Paging list
?>
For more information on PHP related content readers can view the site topics: "PHP array" Operation Techniques Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage Summary", "Php+ajax Tips and Application Summary", "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and " A summary of common PHP database operations tips
I hope this article will help you with the PHP program design.