您好,登錄后才能下訂單哦!
小編給大家分享一下ThinkPHP如何實(shí)現(xiàn)分頁(yè)功能,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
首先在根目錄建一個(gè)Tools文件夾,在Tools文件夾下建Page.class.php類文件,這樣以后自定義的工具類都可放在Tools文件夾下。
此類封裝有以下函數(shù):獲取請(qǐng)求地址,開(kāi)始頁(yè),從哪一條顯示,結(jié)束頁(yè) 從哪一條結(jié)束,頁(yè)碼列表(首頁(yè)超鏈接,上一頁(yè),頁(yè)碼數(shù)字列表超鏈接,下一頁(yè),尾頁(yè),跳轉(zhuǎn)),對(duì)于分頁(yè)足夠使用!
下面是Page.class.php代碼
<?php //命名空間的名稱與上級(jí)目錄tools一致 //原因:當(dāng)前Page.class.php類文件會(huì)被自動(dòng)加載機(jī)制引入 // 在引入的同時(shí)會(huì)把"tools"變?yōu)槲募纳霞?jí)目錄,進(jìn)而獲得該P(yáng)age類文件 namespace Tools; class Page { private $total; //數(shù)據(jù)表中總記錄數(shù) private $listRows; //每頁(yè)顯示行數(shù) private $limit; private $uri; //當(dāng)前鏈接URL private $pageNum; //頁(yè)數(shù) private $config=array('header'=>"個(gè)記錄", "prev"=>"上一頁(yè)", "next"=>"下一頁(yè)", "first"=>"首 頁(yè)", "last"=>"尾 頁(yè)"); private $listNum=8; //限制頁(yè)碼列表數(shù)目 /* * $total 總記錄數(shù) * $listRows 每頁(yè)顯示行數(shù) */ public function __construct($total, $listRows=10, $pa=""){ $this->total=$total; //數(shù)據(jù)表中總記錄數(shù) $this->listRows=$listRows; //設(shè)置每頁(yè)顯示行數(shù) $this->uri=$this->getUri($pa); //請(qǐng)求地址 $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; //當(dāng)前頁(yè) $this->pageNum=ceil($this->total/$this->listRows); //總頁(yè)數(shù) $this->limit=$this->setLimit(); //限制每頁(yè)長(zhǎng)度 } private function setLimit(){ return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}"; } //請(qǐng)求地址 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; } //開(kāi)始頁(yè),從哪一條顯示 private function start(){ if($this->total==0) return 0; else return ($this->page-1)*$this->listRows+1; } //從哪一條結(jié)束 private function end(){ return min($this->page*$this->listRows,$this->total); } //首頁(yè)超鏈接 private function first(){ $html = ""; if($this->page==1) $html.=''; else $html.=" <a href='{$this->uri}&page=1'>{$this->config["first"]}</a> "; return $html; } //上一頁(yè) 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; } //頁(yè)碼數(shù)字列表超鏈接 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; } //下一頁(yè) 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; } //尾頁(yè) 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; } //跳轉(zhuǎn) 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.'" ><input type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> '; } //頁(yè)碼列表 function fpage($display=array(0,1,2,3,4,5,6,7,8)){ $html[0]=" 共有<b>{$this->total}</b>{$this->config["header"]} "; $html[1]=" 每頁(yè)顯示<b>".($this->end()-$this->start()+1)."</b>條,本頁(yè)<b>{$this->start()}-{$this->end()}</b>條 "; $html[2]=" <b>{$this->page}/{$this->pageNum}</b>頁(yè) "; $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; } }
控制器代碼:
//商品列表 function showlist(){ //實(shí)現(xiàn)分頁(yè)效果 $goods = D('goods'); //① 獲得數(shù)據(jù)的總記錄條數(shù) $total = $goods -> count(); //select count(*) from sw_goods; $per = 7; //每頁(yè)顯示7條記錄 //②實(shí)例化分頁(yè)類 $page_obj = new \Tools\Page($total, $per); //③自定義sql語(yǔ)句,獲取每頁(yè)信息 $sql = "select * from sw_goods order by goods_id desc ".$page_obj->limit; $info = $goods->query($sql); //④獲取頁(yè)碼列表 $pagelist = $page_obj->fpage(array(3,4,5,6,7,8)); //分配 $this->assign('pagelist',$pagelist); $this->assign('info',$info); $this->display(); }
前臺(tái)顯示代碼
<{$pagelist}>
看完了這篇文章,相信你對(duì)“ThinkPHP如何實(shí)現(xiàn)分頁(yè)功能”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。