. 1 <? PHP 2 class Page { . 3 Private $ Total ; // number of total recorded data table . 4 Private $ ListRows ; // page number of display lines . 5 Private $ limit ; . 6 Private $ URI ; . 7 Private $ pageNum ; // pages . 8 Private $ config = Array ( 'header' => "record", "prev" => "previous", "next" => "next", "first" => "Home", "last" => "End" ); 9 private $listNum=8; 10 /* 11 * $total 12 * $listRows 13 */ 14 public function __construct($total, $listRows=10, $pa=""){ 15 $this->total=$total; 16 $this->listRows=$listRows; 17 $this->uri=$this->getUri($pa); 18 $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; 19 $this->pageNum=ceil($this->total/$this->listRows); 20 $this->limit=$this->setLimit(); 21 } 22 23 private function setLimit(){ 24 return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; 25 } 26 27 private function getUri($pa){ 28 $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa; 29 $parse=parse_url($url); 30 31 32 33 if(isset($parse["query"])){ 34 parse_str($parse['query'],$params); 35 unset($params["page"]); 36 $url=$parse['path'].'?'.http_build_query($params); 37 38 } 39 40 return $url; 41 } 42 43 function __get($args){ 44 if($args=="limit") 45 return $this->limit; 46 else 47 return null; 48 } 49 50 private function start(){ 51 if($this->total==0) 52 return 0; 53 else 54 return ($this->page-1)*$this->listRows+1; 55 } 56 57 private function end(){ 58 return min($this->page*$this->listRows,$this->total); 59 } 60 61 private function first(){ 62 $html = ""; 63 if($this->page==1) 64 $html.=''; 65 else 66 $html.=" <a href='{$this->uri}&page=1'>{$this->config["first"]}</a> "; 67 68 return $html; 69 } 70 71 private function prev(){ 72 $html = ""; 73 if($this->page==1) 74 $html.=''; 76presence75 $html.=" <a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a> "; 77 78 return $html; 79 } 80 81 private function pageList(){ 82 $linkPage=""; 83 84 $inum=floor($this->listNum/2); 85 86 for($i=$inum; $i>=1; $i--){ 87 $page=$this->page-$i; 88 89 if($page<1) 90 continue; 91 92 $linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> "; 93 94 } 95 96 $linkPage.=" {$this->page} "; 97 98 99 for($i=1; $i<=$inum; $i++){ 100 $page=$this->page+$i; 101 if($page<=$this->pageNum) 102 $linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> "; 103 else 104 break; 105 } 106 107 return $linkPage; 108 } 109 110 private function next(){ 111 $html = ""; 112 if($this->page==$this->pageNum) 113 $html.=''; 114 else 115 $html.=" <a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a> "; 116 117 return $html; 118 } 119 120 private function last(){ 121 $html = ""; 122 if($this->page==$this->pageNum) 123 $html.=''; 124 else 125 $html.=" <a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a> "; 126 127 return $html; 128 } 129 130 private function goPage(){ 131 return ' <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> '; 132 } 133 function fpage($display=array(0,1,2,3,4,5,6,7,8)){ 134 $html[0]=" 共有<b>{$this->total}</b>{$this->config["header"]} "; 135 $html[1]=" 每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 "; 136 $html[2]=" <b>{$this->page}/{$this->pageNum}</b>页 "; 137 138 $html[3]=$this->first(); 139 $html[4]=$this->prev(); 140 $html[5]=$this->pageList(); 141 $html[6]=$this->next(); 142 $html[7]=$this->last(); 143 $html[8]=$this->goPage(); 144 $fpage=''; 145 foreach($display as $index){ 146 $fpage.=$html[$index]; 147 } 148 149 return $fpage; 150 151 } 152 153 154 }
Test page
. 1 ? < PHP 2 // traditional paging achieve the effect 3 // connect to the database to obtain the data, do show pages . 4 . 5 header ( "Content-type: text / HTML; charset = UTF-. 8" ); . 6 $ Link = mysql_connect ( 'localhost', 'the root', '123456' ); . 7 the mysql_select_db ( 'Shop', $ Link ); . 8 the mysql_query ( 'names SET UTF8' ); . 9 10 echo <<< EOF . 11 <style type = "text / CSS "> 12 is Table {width: 700px; border: 1px Solid Black; margin: Auto; border-Collapse:collapse;} 13 is TD {border: 1px Solid Black;} 14 </ style> 15 <Table> 16 <TR style = 'font-weight: Bold'> <TD> ID </ td> <td> name </ td> <td > price </ td> <td> number </ td> <td> wt </ TD> </ TR> . 17 EOF; 18 is . 19 // ① introducing paging class 20 is the include "./page.class.php" ; 21 is 22 // ② obtain the total number of each page show the number of 23 $ SQL = "sw_goods from the SELECT *" ; 24- $ qry = mysql_query ( $ SQL ); 25 $total = mysql_num_rows($qry); // total number of 26 is $ per =. 7; // page number of 27 28 @ ③ class object instantiated tab 29 $ Page = new new Page ( $ Total , $ per ); 30 31 is // ④ set sql statement information obtained per 32 // $ page-> limit: paging class will automatically "limit offset length" current page according to parameter information assembled well 33 is $ SQL3 = "SELECT * from sw_goods by goods_id Order." $ Page -> limit; 34 is $ qry3 = the mysql_query ( $ SQL3 ); 35 36 // ⑤ obtaining page information list 37 $page_list = $page -> fpage(array(3,4,5,6,7,8)); 38 39 $page_num = isset($_GET['page'])?$_GET['page']:1; 40 $num = ($page_num-1)*$per+1; 41 42 while($rst3 = mysql_fetch_assoc($qry3)){ 43 printf("<tr>"); 44 printf("<td>%d</td>",$num); 45 printf("<td>%s</td>",$rst3['goods_name']); 46 printf("<td>%s</td>",$rst3['goods_price']); 47 printf("<td>%d</td>",$rst3['goods_number']); 48 printf("<td>%d</td>",$rst3['goods_weight']); 49 printf("</tr>"); 50 $num++; 51 } 52 printf("<tr><td colspan='5'>%s</td></tr>",$page_list); 53 echo "</table>";