传统分页
Posted caoqh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传统分页相关的知识,希望对你有一定的参考价值。
1 <?php 2 class Page { 3 private $total; //数据表中总记录数 4 private $listRows; //每页显示行数 5 private $limit; 6 private $uri; 7 private $pageNum; //页数 8 private $config=array(‘header‘=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页"); 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.=‘‘; 75 else 76 $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 }
测试页面
1 <?php 2 //传统分页效果实现 3 //连接数据库,获得数据,做分页显示 4 5 header("content-type:text/html;charset=utf-8"); 6 $link = mysql_connect(‘localhost‘,‘root‘,‘123456‘); 7 mysql_select_db(‘shop‘, $link); 8 mysql_query(‘set names utf8‘); 9 10 echo <<<eof 11 <style type="text/css"> 12 table {width:700px; border:1px solid black; margin:auto; border-collapse:collapse;} 13 td {border:1px solid black; } 14 </style> 15 <table> 16 <tr style=‘font-weight:bold‘><td>序号</td><td>名称</td><td>价格</td><td>数量</td><td>重量</td></tr> 17 eof; 18 19 //① 引入分页类 20 include "./page.class.php"; 21 22 //② 获得总条数、每页显示条数 23 $sql = "select * from sw_goods"; 24 $qry = mysql_query($sql); 25 $total = mysql_num_rows($qry); //总条数 26 $per = 7;//每页条数 27 28 //③ 实例化分页类对象 29 $page = new Page($total, $per); 30 31 //④ 设置sql语句获得每页信息 32 //$page->limit:分页类会根据当前页码参数自动把 "limit 偏移量,长度" 信息给拼装好 33 $sql3 = "select * from sw_goods order by goods_id ".$page->limit; 34 $qry3 = mysql_query($sql3); 35 36 //⑤ 获得页码列表信息 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>";
以上是关于传统分页的主要内容,如果未能解决你的问题,请参考以下文章
FragmentStatePagerAdapter 视图分页器片段在活动重新创建后不显示