php分页类
1.需求
学会php分页类的使用
2.参考例子
CI的分页类
3.代码部分
<?php class pagination{ public $pagesize=20; public $pageindex=2; public $total_row=202; public $linkurl='test.php?c=1&m=2'; public $pagestr="&page="; public $pageshownum=22; public function __construct($params = array()) { if (0 < count($params)) { $this->initialize($params); } } public function initialize($params = array()) { if (0 < count($params)) { foreach ($params as $key => $val ) { if (isset($this->$key)) { $this->$key = $val; } } } } public function create_link() { if ($this->total_row <= 0) { return ""; } if ($this->pagesize <= 0) { return ""; } $maxPage=0; if($this->total_row%$this->pagesize === 0) { $maxPage =(int)$this->total_row/$this->pagesize; } else { $maxPage =(int)($this->total_row/$this->pagesize)+1; } if ($maxPage < $this->pageindex) { return ""; } $num = $this->pageshownum; if($num>$maxPage) { $num=$maxPage; } return $this->PageSatte($maxPage, $this->pageshownum, $this->pageindex, $this->total_row, $this->linkurl, $this->pagestr); } private function PageSatte($maxpage,$pageshownum, $pageindex, $docount, $linkurl, $pagetag = "&page") { $num = $pageshownum; if($num>$maxpage) { $num=$maxpage; } $str='<ul class="m-pagination1 clearfix">'; $str.='<span>第'.$pageindex."/".$docount."页</span>"; $str.='<span>共'.$maxpage."页</span>"; if($pageindex!==1) { $str.="<li><a href=".$linkurl.$pagetag."1".">第一页</a></li>"; $str.="<li><a href=".$linkurl.$pagetag.($pageindex-1).">上一页</a></li>"; } for($i=1;$i<($maxpage+1);$i+=$num) { if(($i<=$pageindex) && ($pageindex<($i+$num))) { for($j=$i;$j<($i+$num);$j++) { if($pageindex!==$j) { $str.="<li><a href=".$linkurl.$pagetag.$j.">".$j."</a></li>"; } else { $str.="<li><a>".$j."</a></li>"; } } } } if($pageindex<$maxpage) { $str.="<li><a href=".$linkurl.$pagetag.($pageindex+1).">下一页</a></li>"; $str.="<li><a href=".$linkurl.$pagetag.$maxpage.">最后一页</a></li>"; } return $str; } } $config=array( 'pagesize'=>30, 'pageindex'=>1, 'total_row'=>303, "linkurl"=>'localhost/index.php?c=index&m=index', "pagestr"=>'$page=', 'pageshownum'=>10 ); $obj = new pagination($config); echo $obj->create_link();
4.总结
主要是生成数字那部分的算法和不合理情况的返回空值的设定。