<?php/* * ********************************************* * @ class Name: Page * @ parameter: $myde _total-Total records * $myde _size -Number of records displayed on a page * $myde _page-Current page * $myde _url-Get current URL * @ Feature: Paging implementation * @ Author: Song Hai */class page {Priva Te $myde _total; Total record number private $myde _size; Number of records displayed on one page private $myde _page; Current page private $myde _page_count; Total pages Private $myde _i; Beginning pages private $myde _en; End pages Private $myde _url; Gets the current URL */* * $show _pages * page display format, showing the number of pages linked to $show _pages+1. * such as $show_pages=2 so the page is displayed is [home] [prev] 1 2 3 4 5 [next] [last] * * Private $show _pages; Public function __construct ($myde _total = 1, $myde _size = 1, $myde _page = 1, $myde _url, $show _pages = 2) {$this-&G T;myde_total = $this->numeric ($myde _total); $this->myde_size = $this->numeric ($myde _size); $this->myde_page = $this->numeric ($myde _page); $this->myde_page_count = Ceil ($this->myde_total/$this->myde_size); $this->myde_url = $myde _url; if ($this->myde_total < 0) $this->myde_total = 0; if ($this->myde_page < 1) $this->myde_page = 1; if ($this->myde_page_count < 1) $this->myde_page_count = 1; if ($this->myde_page > $this->myde_page_count) $this->myde_page = $this->myde_page_count; $this->limit = ($this->myde_page-1) * $this->myde_size; $this->myde_i = $this->myde_page-$show _pages; $this->myde_en = $this->myde_page + $show _pages; if ($this->myde_i < 1) {$this->myde_en = $this->myde_en + (1-$this->myde_i); $this->myde_i = 1; if ($this->myde_en > $this->myde_page_count) {$this->myde_i = $this->myde_i-($this-& gt;myde_en-$this->myde_page_count); $this->myde_en = $this->myde_page_count; } if ($this->myde_i < 1) $this->myde_i = 1; }//detects whether the digital private function numeric ($num) {if (strlen ($num)) {if (!preg_match ("/^[0-9]+$/", $nu m)) {$num = 1; } else {$num = substr ($num, 0, 11); }} else {$num = 1; } return $num; }//Address replaces private function Page_replace ($page) {return str_replace ("{page}", $page, $this->myde_url); }//Home Private Function Myde_home () {if ($this->myde_page! = 1) {return "<a href= '". $thi S->page_replace (1). "' title= ' Home > Home </a>"; } else {return "<p> home </p>"; }}//previous Private Function Myde_prev () {if ($this->myde_page! = 1) {return "<a href= '" . $this->page_replace ($this->myde_page-1). "' title= ' prev > prev </a>"; } else { Return "<p> prev </p>"; }}//Next Private Function Myde_next () {if ($this->myde_page! = $this->myde_page_count) { Return "<a href= '". $this->page_replace ($this->myde_page + 1). "' title= ' next page ' > Next </a>"; } else {return ' <p> next </p> '; }}//Last Private Function Myde_last () {if ($this->myde_page! = $this->myde_page_count) { Return "<a href= '". $this->page_replace ($this->myde_page_count). "' title= ' last ' > Last </a>"; } else {return ' <p> last </p> '; }}//Output public function myde_write ($id = ' page ') {$str = "<div id=". $id. ">"; $str. = $this->myde_home (); $str. = $this->myde_prev (); if ($this->myde_i > 1) {$str. = "<p class= ' pageellipsis ' >...</p>"; } for ($i = $this->myde_i; $i <= $this->myde_en; $i + +) { if ($i = = $this->myde_page) {$str. = "<a href= '". $this->page_replace ($i). "' title= '." $i. "Page ' class= ' cur ' > $i </a>"; } else {$str. = "<a href= '". $this->page_replace ($i). "' title= '." $i. "Page > $i </a>"; }} if ($this->myde_en < $this->myde_page_count) {$str. = "<p class= ' pageellipsis ' > ...</p> "; } $str. = $this->myde_next (); $str. = $this->myde_last (); $str. = "<p class= ' Pageremark > Total <b>". $this->myde_page_count. "</b> page <b>". $this->myde_total. "</b> data </p>"; $str. = "</div>"; return $str; }}?>
Configuration Items
<?php$host= "localhost", $db _user= "root", $db _pass= "root"; $db _name= "Demo"; $timezone = "Asia/shanghai"; $link = Mysql_connect ($host, $db _user, $db _pass) mysql_select_db ($db _name, $link); mysql_query ("SET names UTF8"); Header (" content-type:text/html; Charset=utf-8 ");? >
Demo
<?phpinclude_once ("config.php"); require_once (' page.class.php '); Pagination class $showrow = 10; Number of rows displayed on a page $curpage = empty ($_get[' page ')? 1: $_get[' page '; The current page should also handle non-numeric cases with the case of the "? Page={page}"; Paging address, if there is a search condition = "? page={page}&q=". $_get[' Q ']//omitted the link to the MySQL code, the test is added by itself $sql = "SELECT * from article"; $total = Mysql_ Num_rows (mysql_query ($sql)); Record the total number of bars if (!empty ($_get[' page ') && $total! = 0 && $curpage > ceil ($total/$showrow)) $curpage = CE Il ($total _rows/$showrow); The current number of pages is greater than the last page//fetch data $sql. = "LIMIT". ($curpage-1) * $showrow. ", $showrow;"; $query = mysql_query ($sql);? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
PHP Page-Out class