php通用分页种
Jun 13, 2016 pm 01:03 PM
function
gt
page
param
this
php通用分页类
<?php interface ILink { public function parse($page,$param); } ?>
登入後複製
?
<?php require'ILink.php'; class LinkAdapter implements ILink { /** * @param unknown_type $page * @param unknown_type $param */ public function parse($page, $param) { $temp="共{$page->getAllPage()}页,第{$page->getCurrentPage()}页 "; $links=$this->getLinkString($param); if($page->hasPrevious())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()-1).%22">上一页</a> ";else{$temp.="上一页 ";} for($i=$page->getCurrentPage();$igetAllPage()&&$igetPerRecords();$i++) { $temp.="<a href="%24links=%24i">{$i}</a> "; } if($page->hasNext())$temp.="<a href="%24links=%22.(%24page->getCurrentPage()+1).%22">下一页</a> ";else{$temp.="下一页 ";} return $temp; } public function getLinkString($param) { $str=""; $attr=$_GET; unset($attr[$param]); if($attr) { foreach($attr as $key=>$val) { if($str=="") { $str.="?$key=$val"; } else { $str.="&$key=$val"; } } $str.="&$param"; } else { $str.="?$param"; } return $str; } } ?>
登入後複製
??
<?php class Page { private $allPage;#总页数 private $allRecords;#总记录数 private $perRecords;#单页记录数 private $currentPage=1;#当前页面 /** * @return the $allPage */ public function getAllPage() { return $this->allPage; } /** * @return the $allRecords */ public function getAllRecords() { return $this->allRecords; } /** * @return the $perRecords */ public function getPerRecords() { return $this->perRecords; } /** * @return the $currentPage */ public function getCurrentPage() { return $this->currentPage; } /** * @param $allPage the $allPage to set */ public function setAllPage($allPage) { $this->allPage = ($allPage%$this->perRecords == 0)?($allPage/$this->perRecords):($allPage/$this->perRecords+1); $this->allPage=intval($this->allPage); } /** * @param $allRecords the $allRecords to set */ public function setAllRecords($allRecords) { $this->allRecords = $allRecords; } /** * @param $perRecords the $perRecords to set */ public function setPerRecords($perRecords) { $this->perRecords = $perRecords; } /** * @param $currentPage the $currentPage to set */ public function setCurrentPage($currentPage) { if ($currentPage currentPage = 1; else if ($currentPage > $this->allPage) $this->currentPage =$this->allPage; else $this->currentPage=$currentPage; } public function hasNext() { return $this->currentPageallPage; } public function hasPrevious() { return $this->currentPage>1; } public function getEndIndex() { return ((($this->currentPage-1)*$this->perRecords)+$this->perRecords)>$this->allRecords?((($this->currentPage-1)*$this->perRecords)+$this->perRecords)-$this->allRecords:$this->perRecords; } public function getStartIndex() { return ($this->currentPage-1)*$this->perRecords; } } ?>
登入後複製
?
<?php require'Page.php'; require'LinkAdapter.php'; class Pager { private $list=array(); private $page;#分页对象 private $param;#页面请求参数 public function __construct($list) { $this->list=$list; $this->page=new Page(); } /** * * @param unknown_type $rows 显示的数据量 * @param unknown_type $current 当前页 */ public function init($rows=5,$current) { $this->page->setAllRecords(count($this->list)); $this->page->setPerRecords($rows); $this->page->setAllPage(count($this->list)); $this->page->setCurrentPage($current); $this->list=array_slice($this->list,$this->page->getStartIndex(),$this->page->getEndIndex()); } /** * 获取分页变量 */ public function getVar() { return $this->list; } /** * @return the $param */ public function getParam() { return $this->param; } /** * @param $param the $param to set */ public function setParam($param) { $this->param = $param; } /** * 加载插件信息,获取生成的链接,装饰器模式 * @param unknown_type $link */ public function getLink($link=null) { if(!empty($link)||!(($link instanceof ILink)))$link=new LinkAdapter(); return $link->parse($this->page,$this->param); } } ?>
登入後複製
?
<?php include'lib/Pager.php'; $target=array(); for($i=0;$i<=100;$i++){$target[]=$i;} $page=new Pager($target); $page->setParam("page"); $page->init(30,$_REQUEST['page']); $list=$page->getVar(); foreach($list as $val): echo $val.'<br>'; endforeach; echo $page->getLink(); ?>
登入後複製
?下载
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)