php数组分页类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 class ArrayPage{ 3 public $totalPage;//全部页数 4 public $lists;//每页显示数目 5 public $arr = array();//分页的数组 6 public $url = '';//分页地址 7 public $order; 8 public $_error = '一切正常';//报错提示 9 public function __construct(array $arr,$lists = 10,$url,$order =0){ 10 if( empty($arr) ){ 11 $this->_error = '数组不存在'; 12 return false; 13 } 14 if($url == ''){ 15 $this->_error = '域名地址不可缺少'; 16 return false; 17 } 18 $this->lists = $lists; 19 $this->url = $url; 20 $this->arr = $arr; 21 $count = count($arr); 22 $this->totalPage = ceil($count / $this->lists); 23 } 24 25 public function getData(){ 26 $page = empty($_GET['page'])?1:$_GET['page']; 27 $start = ($page - 1)*$this->lists; 28 //return $this->lists; 29 if($this->order == 1){ 30 $this->arr = array_reverse($this->arr); 31 } 32 $res = array_slice($this->arr,$start,$this->lists); 33 return $res; 34 } 35 36 public function getShow(){ 37 $page = empty($_GET['page'])?1:$_GET['page']; 38 if($page > 1){ 39 $prevPage = $page-1; 40 }else{ 41 $prevPage = 1; 42 } 43 44 if($page < $this->totalPage){ 45 $nextPage = $page+1; 46 }else{ 47 $page = $this->totalPage; 48 } 49 $str = '<div style="border:1px; width:300px; height:30px; color:#9999CC">'; 50 $str .= "<span>共 {$this->totalPage} 页 / 第 {$page} 页</span>"; 51 $str .= "<span><a href='{$this->url}?page=1'> 首页 </a></span>"; 52 if($page == 1){ 53 $str .= "<span> 上一页 </span>"; 54 }else{ 55 $str .= "<span><a href='{$this->url}?page={$prevPage}'> 上一页 </a></span>"; 56 } 57 if($page == $this->totalPage){ 58 $str .= "<span>下一页 </span>"; 59 }else{ 60 $str .= "<span><a href='{$this->url}?page={$nextPage}'>下一页 </a></span>"; 61 } 62 $str .= "<span><a href='{$this->url}?page={$this->totalPage}'>尾页 </a></span>"; 63 $str .= '</div>'; 64 return $str; 65 } 66 67 public function getError(){ 68 return $this->_error; 69 } 70 } 71 for($i =0;$i<100;$i++){ 72 $arr[$i] = $i*10; 73 } 74 $page = new ArrayPage($arr,10,'http://localhost/testSvn/arrayPage.php',0); 75 $res = $page->getData(); 76 echo $page->getShow();