The functional implementations introduced in the previous articles (upload, thumbnails, verification codes, automatic verification forms) are all based on the classes encapsulated by the ThinkPHP framework, so this time I wrote a pagination class myself to use in the framework.
First, create a Tools folder in the root directory and create a class file in the Tools folder. In this way, all customized tool classes can be placed in the Tools folder in the future.
This type of encapsulates the following functions: get the request address, the start page, which one is displayed, the end page from which one is ended, the page number list (home page hyperlink, previous page, page number list hyperlink, next page, last page, jump), which is sufficient for paging!
Below is the code
<?php //The name of the namespace is the same as the previous directory tools//Reason: The current class file will be introduced by the automatic loading mechanism// At the same time, "tools" will be turned into the file's superior directory, and then the Page file will be obtainednamespace Tools; class Page { private $total; //Total records in the data table private $listRows; //The number of rows displayed per page private $limit; private $uri; //Current link URL private $pageNum; //Page number private $config=array('header'=>"Records", "prev"=>"Previous Page", "next"=>"Next Page", "first"=>"front page", "last"=>"Last Page"); private $listNum=8; //Limit the number of page numbers list /* * $total Total records * $listRows Number of rows displayed per page */ public function __construct($total, $listRows=10, $pa=""){ $this->total=$total; //Total records in the data table $this->listRows=$listRows; //Set the number of rows displayed per page $this->uri=$this->getUri($pa); //Request address $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; //Current page $this->pageNum=ceil($this->total/$this->listRows); //Total page count $this->limit=$this->setLimit(); //Limit the length per page } private function setLimit(){ return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; } //Request address 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; } //Which one is displayed on the start page private function start(){ if($this->total==0) return 0; else return ($this->page-1)*$this->listRows+1; } // Which one ends from private function end(){ return min($this->page*$this->listRows,$this->total); } //Home page hyperlink private function first(){ $html = ""; if($this->page==1) $html.=''; else $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=1'>{$this->config["first"]}</a>&nbsp;&nbsp;"; return $html; } //Previous page private function prev(){ $html = ""; if($this->page==1) $html.=''; else $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page-1)."'>{$this->config["prev"]}</a>&nbsp;&nbsp;"; return $html; } //Page number list hyperlink private function pageList(){ $linkPage=""; $inum=floor($this->listNum/2); for($i=$inum; $i>=1; $i--){ $page=$this->page-$i; if($page<1) continue; $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;"; } $linkPage.="&nbsp;{$this->page}&nbsp;"; for($i=1; $i<=$inum; $i++){ $page=$this->page+$i; if($page<=$this->pageNum) $linkPage.="&nbsp;<a href='{$this->uri}&page={$page}'>{$page}</a>&nbsp;"; else break; } return $linkPage; } //Next page private function next(){ $html = ""; if($this->page==$this->pageNum) $html.=''; else $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->page+1)."'>{$this->config["next"]}</a>&nbsp;&nbsp;"; return $html; } //Last page private function last(){ $html = ""; if($this->page==$this->pageNum) $html.=''; else $html.="&nbsp;&nbsp;<a href='{$this->uri}&page=".($this->pageNum)."'>{$this->config["last"]}</a>&nbsp;&nbsp;"; return $html; } //Jump private function goPage(){ return '&nbsp;&nbsp;<input type="text" onkeydown="javascript:if(==13){var page=(>'.$this->pageNum.')?'.$this->pageNum.':;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px"><input type="button" value="GO" onclick="javascript:var page=(>'.$this->pageNum.')?'.$this->pageNum.':;location=\''.$this->uri.'&page=\'+page+\'\'">&nbsp;&nbsp;'; } //Page Number List function fpage($display=array(0,1,2,3,4,5,6,7,8)){ $html[0]="&nbsp;&nbsp;There are a total of<b>{$this->total}</b>{$this->config["header"]}&nbsp;&nbsp;"; $html[1]=" Show per page<b>".($this->end()-$this->start()+1)."</b>strip,This page<b>{$this->start()}-{$this->end()}</b>strip&nbsp;&nbsp;"; $html[2]="&nbsp;&nbsp;<b>{$this->page}/{$this->pageNum}</b>Page&nbsp;&nbsp;"; $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:
//Product List function showlist(){ //Implement paging effect $goods = D('goods'); //① Total number of records obtained for data $total = $goods -> count(); //select count(*) from sw_goods; $per = 7; // Show 7 records per page //②Instantiate the paging class $page_obj = new \Tools\Page($total, $per); //③Custom sql statements to obtain information per page $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit; $info = $goods->query($sql); //④ Get the page number list $pagelist = $page_obj->fpage(array(3,4,5,6,7,8)); //distribute $this->assign('pagelist',$pagelist); $this->assign('info',$info); $this->display(); }
Front desk display code
<{$pagelist}>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.