本文实例讲述了php实现对数组分页处理方法。分享给大家供大家参考,具体如下:
最近用到了用数组数据分页,这里整理了一下,具体代码如下:
<?php
class paginationarray{
public $pagearray=array(); //数组
public $pagesize=10; //每页显示记录数
public $current= 1; //当前页
private $total=0; //总页数
private $prev=0; //上一页
private $next=0; //下一页
public $argumetsother='';//设置参数
function __construct($array=array(),$pagesize=10,$current=1){
$this->pagearray=$array;
$this->pagesize=$pagesize;
$this->current=$current;
}
/*通过数组进行初始化
*
* 数组为关联数组,参数索引为pagearray,pagesize,current
*
*/
function setarguments($arr){
if (is_array($arr)){
$this->pagearray=$arr['pagearray'];
$this->pagesize=$arr['pagesize'];
$this->current=$arr['current'];
}else{
return ;
}
}
//返回链接
function page(){
$_return=array();
/*calculator*/
$this->total=ceil(count($this->pagearray)/$this->pagesize);
$this->prev=(($this->current-1)<= 0 ? "1":($this->current-1));
$this->next=(($this->current+1)>=$this->total ? $this->total:$this->current+1);
$current=($this->current>($this->total)?($this->total):$this->current);
$start=($this->current-1)*$this->pagesize;
$arrleng=count($this->pagearray);
for($i=$start;$i<($start+$this->pagesize);$i++){
if($i >= $arrleng)break;
array_push($_return,$this->pagearray[$i]);
}
$pagearray["source"]=$_return;
$pagearray["links"]=$this->linkstyle(2);
return $pagearray;
}
//链接的样式
private function linkstyle($number=1){
$linkstyle='';
switch ($number){
case 1:
$linkstyle="<a href="?page=1">first</a> <a href="?page={$this->prev}">prev</a> <a href="?page={$this->next}">next</a> <a href="?page={$this->total}">end</a>";
break;
case 2:
$linkstyle="<a href="?page=1&{$this->argumetsother}">首页</a> <a href="?page={$this->prev}&{$this->argumetsother}">上一页</a> <a href="?page={$this->next}&{$this->argumetsother}">下一页</a> <a href="?page={$this->total}&{$this->argumetsother}">尾页</a>";
break;
}
return $linkstyle;
}
}
//调用的实例
/*
header('content-type: text/html;charset=utf-8');
$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
$page= isset($_get['page'])? $_get['page'] : 1 ;
$arraypage = new paginationarray($array,"5",$page);
$r = $arraypage->page();
foreach($r["source"] as $s){
echo $s.'<br />';
}
echo $r["links"];
*/
?>
更多关于php相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php网络编程技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!