另一种方式的PHP分页类
本站原创作品,转载请注明出处:http://www.cnblogs.com/keheng/articles/2494282.html
前几天在做一个后台分类的时候,碰到了一个小问题。
在读取大类后也要把小类显示出来,但是分页即失去了作用,今天突发奇想,能不能把从数据库读取的array,按分页效果进行分页显示,这样就可以解决先前遇到的那个问题。
从数据库读取出来的数据的数组结构:
1 array( 2 3 array( 4 5 'id' => 'aa', 6 7 'a' => 'bb', 8 9 'b' => 'cc', 10 11 'c' => 'dd', 12 13 ), 14 15 )
就读取这样的数据结构来做了一个简单的数组分页显示类:
1 class ArrayPage{ 2 public $page; 3 public $pagesize; 4 public $pagecount; 5 public $thisarray; 6 public $filename; 7 8 public function __construct($rs,$filename,$pagesize){ 9 if(!is_array($rs)){ 10 exit('必须要数组!'); 11 } 12 $this->thisarray = $rs; 13 $this->filename = $filename; 14 $this->pagesize = $pagesize; 15 $this->page = $_GET[page]; 16 $this->pagecount = ceil(count($rs)/$pagesize); 17 $this->judgmentpage(); 18 $this->display(); 19 } 20 21 public function __destruct(){ 22 unset($this->thisarray,$this->filename,$this->pagesize,$this->page,$this->pagecount); 23 } 24 25 public function display(){ 26 $str_t = '<table width="100%" border="0" cellspacing="2" cellpadding="0" style="font-size:12px;text-align:center;"> 27 <tr><th>id</th><th>aa</th><th>bb</th><th>cc</th><th>dd</th><th>ee</th><th>ff</th><th>gg</th></tr>'; 28 $star=($this->page-1)*$this->pagesize; 29 $end=$this->page*$this->pagesize-1; 30 //echo '<!--'.'($epage-1)*$pagesize='.(($epage-1)*$pagesize).' - $epage+$pagesize-1='.($epage*$pagesize-1).'-->'; 31 for($i=$star;$i<=$end;$i++){ 32 if($i<count($this->thisarray)){ 33 $str.= '<tr>'."\n"; 34 $value=$this->thisarray[$i]; 35 $str.= '<td style="background:#e2e2e2;height:30px;">'.$i.'</td>'."\n"; 36 foreach($value as $k => $v){ 37 $str.='<td style="background:#e2e2e2;height:30px;">'.'<span style="color:red;">'.$i.'</span>'.$value[$k].'</td>'."\n"; 38 } 39 $str.='</tr>'."\n"; 40 } 41 } 42 $str_p= '<tr><td colspan="8">'.$this->Paging('testArrayPage.php?',$this->pagecount,$this->page).'</td></tr></table>'; 43 echo $str_t.$str.$str_p; 44 } 45 46 47 function judgmentpage(){ 48 if($this->page!=null && isset($this->page) && is_numeric($this->page)){ 49 if($this->page<=0){ 50 $epage=1; 51 }elseif($this->page>count($this->thisarray)){ 52 $epage=count($this->thisarray); 53 }else{ 54 $epage=$this->page; 55 } 56 }else{ 57 $epage=1; 58 } 59 $this->page=$epage; 60 } 61 62 function Paging($FilesName,$PageCount,$page){ //=============== 分页 =============== 63 $PageStr=""; 64 $topname='首页'; 65 $bottomname='尾页'; 66 $overname='上一页'; 67 $upname='下一页'; 68 $p=$FilesName.'page='; 69 if ($PageCount>1){ 70 if ($page<=1){ 71 $page=1; 72 $PageStr='当前第 '.$page.' / '.$PageCount.' 页 ['.$topname.'] ['.$overname.'] <a href="'.$p.($page+1).'">['.$upname.']</a> <a href="'.$p.($PageCount).'">['.$bottomname.']</a>'; 73 }else if($page>=$PageCount){ 74 $page=$PageCount; 75 $PageStr='当前第 '.$page.' / '. $PageCount . ' 页 <a href="'.$p.(1).'">['.$topname.']</a> <a href="'.$p.($page-1).'">['.$overname.']</a> ['.$upname.'] ['.$bottomname.']'; 76 }else{ 77 $PageStr='当前第 ' . $page . ' / '. $PageCount . ' 页 <a href="'.$p.(1).'">['.$topname.']</a> <a href="'.$p.($page-1).'">['.$overname.']</a> <a href="'.$p.($page+1).'">['.$upname.']</a> <a href="'.$p.($PageCount).'">['.$bottomname.']</a>'; 78 } 79 }else{ 80 $PageCount=1; 81 $page=1; 82 $PageStr=('当前第 ' . $page) . ' / '. $PageCount . ' 页 ['.$topname.'] ['.$overname.'] ['.$upname.'] ['.$bottomname.']'; 83 } 84 return $PageStr; 85 } 86 }
通过一行命令即可把数组转化为分页效果
1 new Arraypage(myArray(),'TestArrayPage.php?',13);