本文发布于:2012-09-15,最后更新于:2021-02-09,如果内容失效请留言告知。
在列表翻页实现中常会用到分页设计,这是一个简单易用的php的分页类,摘录参考。(来自《PHP典型模块与项目实战大全》第17章)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | <? /**************************************/ /* @ Pager */ /* @ */ /* @ Designer AnVy */ /**************************************/ class listPage { //public var $firstPage; var $endPage; var $lastPage; var $nextPage; var $thisPage; var $pageSize; var $rowNum; var $listNum; var $cssClass; var $splitStr; var $pageLast; var $pageNext; var $pageFirst; var $pageEnd; var $pageList; var $pageSelect; var $pageAll; var $links; function __construct($rowNum,$pageSize = '10',$listNum = '8') { $this->rowNum = $rowNum; $this->pageSize = $pageSize; $this->listNum = $listNum; $this->splitStr = ' '; $this->lastPage = '上一页'; $this->nextPage = '下一页'; $this->firstPage = '首页'; $this->endPage = '末页'; $this->thisPage = $_GET['page'] ? $_GET['page'] : 1; } function getStrWhere() { $temp = array(); foreach ($_GET as $key=>$val) if($key != 'page') $temp[] = urlencode($key) . '=' . urlencode($val); foreach ($_POST as $key=>$val) $temp[] = urlencode($key) . '=' . urlencode($val); $str = @ join('&',$temp); unset($temp); return $str; } function getPageNum() { if ($this->thisPage < 1 ) $this->thisPage = 1; if ($this->rowNum < 1 ) return 1; return ceil($this->rowNum/$this->pageSize); } function createSelect($s,$e) { $temp = array(); $temp[] = '< select class="'.$this->cssClass.'" onchange="window.location=this.options[this.selectedIndex].value">'; for($i = $s ; $i <= $e;$i ++) $temp[] = '< option value="? page = '.$i.' &'.$this->getStrWhere().'" '.(($i == $this->thisPage)?'selected':'').'>'.$i; $temp[] = '< select >'; return join(chr(13),$temp); } function createPage() { $strWhere = $this->getStrWhere(); $lastPage = $this->lastPage; $firstPage = $this->firstPage; $endPage = $this->endPage; $nextPage = $this->nextPage; $thisPage = $this->thisPage; $pageNum = $this->getPageNum(); $listNum = $this->listNum; $startNum = $thisPage - (ceil($listNum/2)-1); if ($startNum < 1 ) $ startNum = 1 ; $endNum = $startNum + $listNum - 1; if ($endNum > $pageNum) $endNum = $pageNum; $this->links['first'] ='?'.$strWhere.'&page=1'; $this->links['last'] ='?'.$strWhere.'&page='.(($thisPage-1< 1 )?1:($thisPage-1)); $this->links['next'] ='?'.$strWhere.'&page='.(($thisPage+1>$pageNum)?$pageNum:($thisPage+1)); $this->links['end'] ='?'.$strWhere.'&page='.$pageNum; $this->pageFirst = '< a href="'.$this->links['first'].'" class="'.$this->cssClass.'">'.$firstPage.'</ a >'.$this->splitStr; $this->pageLast = '< a href="'.$this->links['last'].'" class="'.$this->cssClass.'">'.$lastPage.'</ a >'; $this->pageNext = '< a href="'.$this->links['next'].'" class="'.$this->cssClass.'">'.$nextPage.'</ a >'; $this->pageEnd = $this->splitStr.'< a href="'.$this->links['end'].'" class="'.$this->cssClass.'">'.$endPage.'</ a >'; $pageList; for($i = $startNum ; $i <= $endNum ; $i ++) if ($thisPage != $i) $pageList[] = '< a href = "?'.$strWhere.'&page='.$i.'" class="'.$this->cssClass.'">'.$i.'</ a >'; else $pageList[] = '< a class="'.$this->cssClass.'">< b >'.$i.'</ b ></ a >'; $this->pageList = @ join($this->splitStr,$pageList); $this->pageSelect = $this->createSelect($startNum,$endNum); $this->pageAll = $this->pageFirst.$this->splitStr.$this->pageLast.$this->splitStr.$this->pageList.$this->splitStr.$this->pageNext.$this->splitStr.$this->pageEnd.$this->splitStr.$this->pageSelect; } } ?> |
用法示例:
1 2 3 | $obj = new listPage(22222222); $obj->createPage(); echo $obj->pageAll; |
我又来了,真的喜欢上楼主的文章了,很吸引人,比一般的文章感觉更真实,读起来很贴近生活,楼主继续努力,你拥有我们这些忠实的粉丝!加油!
很难搞
哈,php什么的,不懂。。。
[reply=任侠,2012-09-20 05:02 PM]语言已其实都差不多
最近实习在搞这个[/reply]
大哥为什么不配个图呢?