php数组分页类

时间:2023-03-08 17:40:54
php数组分页类
 <?php
class ArrayPage{
public $totalPage;//全部页数
public $lists;//每页显示数目
public $arr = array();//分页的数组
public $url = '';//分页地址
public $order;
public $_error = '一切正常';//报错提示
public function __construct(array $arr,$lists = 10,$url,$order =0){
if( empty($arr) ){
$this->_error = '数组不存在';
return false;
}
if($url == ''){
$this->_error = '域名地址不可缺少';
return false;
}
$this->lists = $lists;
$this->url = $url;
$this->arr = $arr;
$count = count($arr);
$this->totalPage = ceil($count / $this->lists);
} public function getData(){
$page = empty($_GET['page'])?1:$_GET['page'];
$start = ($page - 1)*$this->lists;
//return $this->lists;
if($this->order == 1){
$this->arr = array_reverse($this->arr);
}
$res = array_slice($this->arr,$start,$this->lists);
return $res;
} public function getShow(){
$page = empty($_GET['page'])?1:$_GET['page'];
if($page > 1){
$prevPage = $page-1;
}else{
$prevPage = 1;
} if($page < $this->totalPage){
$nextPage = $page+1;
}else{
$page = $this->totalPage;
}
$str = '<div style="border:1px; width:300px; height:30px; color:#9999CC">';
$str .= "<span>共 {$this->totalPage} 页 / 第 {$page} 页</span>";
$str .= "<span><a href='{$this->url}?page=1'> 首页 </a></span>";
if($page == 1){
$str .= "<span> 上一页 </span>";
}else{
$str .= "<span><a href='{$this->url}?page={$prevPage}'> 上一页 </a></span>";
}
if($page == $this->totalPage){
$str .= "<span>下一页 </span>";
}else{
$str .= "<span><a href='{$this->url}?page={$nextPage}'>下一页 </a></span>";
}
$str .= "<span><a href='{$this->url}?page={$this->totalPage}'>尾页 </a></span>";
$str .= '</div>';
return $str;
} public function getError(){
return $this->_error;
}
}
for($i =0;$i<100;$i++){
$arr[$i] = $i*10;
}
$page = new ArrayPage($arr,10,'http://localhost/testSvn/arrayPage.php',0);
$res = $page->getData();
echo $page->getShow();