Home > Article > Backend Development > How to implement paging function in ThinkPHP
This article mainly introduces the relevant code of ThinkPHP to implement the paging function in detail. It has certain reference value. Interested friends can refer to the previous articles
(upload, thumbnail, Verification code, automatic verification form) The functional implementations introduced in the article are all implemented based on the classes encapsulated by the ThinkPHP framework, so this time I wrote a paging class to use in the framework.
First create a Tools folder in the root directory, and create the Page.class.php class file under the Tools folder, so that customized tool classes can be placed under the Tools folder in the future.
This type of package has the following functions: Get the request address, start page, which line to display, end page to end from, page number list (homepage hyperlink, previous page, page number number list hyperlink, next One page, last page, jump), enough for paging!
The following is the Page.class.php code
<?php //命名空间的名称与上级目录tools一致 //原因:当前Page.class.php类文件会被自动加载机制引入 // 在引入的同时会把"tools"变为文件的上级目录,进而获得该Page类文件 namespace Tools; class Page { private $total; //数据表中总记录数 private $listRows; //每页显示行数 private $limit; private $uri; //当前链接URL private $pageNum; //页数 private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页"); private $listNum=8; //限制页码列表数目 /* * $total 总记录数 * $listRows 每页显示行数 */ public function __construct($total, $listRows=10, $pa=""){ $this->total=$total; //数据表中总记录数 $this->listRows=$listRows; //设置每页显示行数 $this->uri=$this->getUri($pa); //请求地址 $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; //当前页 $this->pageNum=ceil($this->total/$this->listRows); //总页数 $this->limit=$this->setLimit(); //限制每页长度 } private function setLimit(){ return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; } //请求地址 private function getUri($pa){ $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa; $parse=parse_url($url); if(isset($parse["query"])){ parse_str($parse['query'],$params); unset($params["page"]); $url=$parse['path'].'?'.http_build_query($params); } return $url; } function __get($args){ if($args=="limit") return $this->limit; else return null; } //开始页,从哪一条显示 private function start(){ if($this->total==0) return 0; else return ($this->page-1)*$this->listRows+1; } //从哪一条结束 private function end(){ return min($this->page*$this->listRows,$this->total); } //首页超链接 private function first(){ $html = ""; if($this->page==1) $html.=''; else $html.=" <a href='{$this->uri}&page=1'>{$this->config["first"]}</a> "; return $html; } //上一页 private function prev(){ $html = ""; if($this->page==1) $html.=''; else $html.=" <a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a> "; return $html; } //页码数字列表超链接 private function pageList(){ $linkPage=""; $inum=floor($this->listNum/2); for($i=$inum; $i>=1; $i--){ $page=$this->page-$i; if($page<1) continue; $linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> "; } $linkPage.=" {$this->page} "; for($i=1; $i<=$inum; $i++){ $page=$this->page+$i; if($page<=$this->pageNum) $linkPage.=" <a href='{$this->uri}&page={$page}'>{$page}</a> "; else break; } return $linkPage; } //下一页 private function next(){ $html = ""; if($this->page==$this->pageNum) $html.=''; else $html.=" <a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a> "; return $html; } //尾页 private function last(){ $html = ""; if($this->page==$this->pageNum) $html.=''; else $html.=" <a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a> "; return $html; } //跳转 private function goPage(){ return ' <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> '; } //页码列表 function fpage($display=array(0,1,2,3,4,5,6,7,8)){ $html[0]=" 共有<b>{$this->total}</b>{$this->config["header"]} "; $html[1]=" 每页显示<b>".($this->end()-$this->start()+1)."</b>条,本页<b>{$this->start()}-{$this->end()}</b>条 "; $html[2]=" <b>{$this->page}/{$this->pageNum}</b>页 "; $html[3]=$this->first(); $html[4]=$this->prev(); $html[5]=$this->pageList(); $html[6]=$this->next(); $html[7]=$this->last(); $html[8]=$this->goPage(); $fpage=''; foreach($display as $index){ $fpage.=$html[$index]; } return $fpage; } }
Controller code:
//商品列表 function showlist(){ //实现分页效果 $goods = D('goods'); //① 获得数据的总记录条数 $total = $goods -> count(); //select count(*) from sw_goods; $per = 7; //每页显示7条记录 //②实例化分页类 $page_obj = new \Tools\Page($total, $per); //③自定义sql语句,获取每页信息 $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit; $info = $goods->query($sql); //④获取页码列表 $pagelist = $page_obj->fpage(array(3,4,5,6,7,8)); //分配 $this->assign('pagelist',$pagelist); $this->assign('info',$info); $this->display(); }
Front-end display code
5b4f1099f9c392e40df600c9df44893c
The above is the entire content of this article, I hope it will help everyone learn Helps.
Related recommendations:
php extension to mongodb (first acquaintance)_javascript skills
php extension to mongodb (little test)_javascript skills
Analysis of the difference between php image generation functions_basic knowledge
The above is the detailed content of How to implement paging function in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!