PHP Common pagination Class Code instance

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags .url class code display function get pagination paging

PHP Tutorials Common pagination Class code

*/
Class Dividepage//Paging class
{
Private $page; Current page
Private $total; Total number of records
Private $pagesize; Number of records per page
Private $prepage; Previous page
Private $nextpage; Next page
Private $lastpage; Last page
Private $url; Url
Private $start; When the page displays the number of records to start
Private $ENDP; When the page displays the number of records end bars
Private $first; Query start number

Constructors
Public function __construct ($total, $pagesize, $url)
{
$this->total = intval ($total);
$this->pagesize = intval ($pagesize);
$this->url = $url;
$this->init ();
}

 //Initialization Paging information
 public function init ()
 {
  $parse _url = Parse_url ($this->url);
 //print_r ($parse _url);
  $url _query = $parse _url[' query ';
  if (empty ($url _query)
  {
   $this->page = 1;
   $this->url = $ This->url. Page= ';
 }
  Else
  {
   Preg_match ("/(^|&) page= (. *) (&|$) UI", $url _query, $temp);
   $this->page = Intval ($temp [2]);
   $this->url=preg_replace ("/page= (. *)/", "page=", $this->url);
 }

  $this->lastpage = ceil ($this->total/$this->pagesize) ceil ($this->total/$this->pagesize) : 1;
 //Last page number, total/per page UP, if the total is 0, then the last page is the first page.
  $this->page = min ($this->lastpage, $this->page) >0 min ($this->lastpage, $this->page): 1;
 //The current page is greater than the last page, the last page is the current page, not greater than 0, then 1 is the current page.
  $this->first = ($this->page-1) * $this->pagesize//Calculation SQL query start position
  $this->prepage = $ This->page-1;                       //Previous
  $this->nextpage = $this->page+1;      //Next
  $this->start = $this->total? $this->first +1:0; //When page records start numbering
  $this->ENDP = min ($this->total, $this->page* $this->pagesize);//When the page records end number
 

Public Function GetPageInfo ()//Get the current page basic information, such as: Display 第1-5条 record, a total of 9 records.
{
Return ' <span class= pageinfostyle > Show <span class = ' NumberStyle ' > '. $this->start. $this->ENDP .' </span> Records, total <span class = "NumberStyle" > '. $this->total. ' </span> Records. </span> ';
}

Public Function Getpagenav ()//Get page navigation such as: First Prev Next last
{
$nav = "";
if ($this->lastpage = = 1)
{
return $nav;
Break;
}
$nav = ' <a href = '. $this->url. ' 1 ' class = ' navpagestyle ' > Home </a> ';
if ($this->prepage)
{
$nav. = ' <a href = '. $this->url. $this->prepage. ' class = ' Navpagestyle ' > Prev </a> ';
}
Else
{
$nav. = ' <a class = ' Navpagestyle ' > Prev </a> ';
}

  if ($this->nextpage <= $this->lastpage)
  {
   $nav. = ' <a href = '. $this- >url. $this->nextpage. ' class = Navpagestyle > Next </a> ';
 }
  Else
  {
   $nav. = ' <a class = ' Navpagestyle ' > next page </a> ';
 }
  $nav. = ' <a href = '. $this->url. $this->lastpage. "class =" Navpagestyle "> Last </a>";
  return $nav;
 }

Public Function Getjumpnav ()//dropdown Jump Paging
{
$nav = ' <span class = ' Pageinfostyle ' > to <select name= "Jumpnav" size= "1" class = "Jumpnav" onchange = ' window.location = "'. $this->url." +this.value ' > '. n ";
For ($i =1 $i <= $this->lastpage; $i + +)
{
if ($i = = $this->page)
{
$nav. = ' <option value = '. $i. ' selected> '. $i. ' </option> '. n ";
}
Else
{
$nav. = ' <option value = '. $i. ' > ' $i. ' </option> '. n ";
}

}
$nav. = ' </select> page, a total of <span class= ' NumberStyle ' > '. $this->lastpage. ' </span> page </span> ';
return $nav;
}

Public Function Getlimit ()
{
Return $this->first. $this->pagesize;
}

Public Function Getallnav ()//Get Navigation
{
return $this->getpageinfo () $this->getpagenav (). $this->getjumpnav ();
}
}

Usages:
$sql 1 = mysql tutorial _query ("SELECT * from Sun_message");
$total =mysql_num_rows ($sql 1);//Total number of data queried
$pagesize = 5;//The number of record bars displayed per page
$url = uri of $_server[' Request_uri '];//request

$pageclass = new Dividepage ($total, $pagesize, $url); Create a paging class (class can be initialized automatically)
$limit = $pageclass->getlimit ()///Get the starting number of the record to display for the current page and the number of display bars per page, such as: 0, 5 (show 5 records starting from 0)
echo $dividepageclass->getallpagenav ();//display all pager bars, such as display 第11-13条 record, total 13 records. Home previous next last to 1th page, total 3 pages

$sql 2 = "SELECT * from Sun_message ORDER BY mid desc limit {$limit}"; Paging content output
$result =mysql_query ($sql);
while ($row =mysql_fetch_array ($result))
{
echo "<hr><b>". $row [title]. | ". $row [author];
}

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.