PHP之簡單的分頁類
阿新 • • 發佈:2018-08-02
lang ger star try select tar -h code doc pageClass.php
<?php class Page{ private $total;//總條數 private $size;//每頁條數 private $pageCount;//總頁數 private $currentPage;//當前頁碼 private $showPages = 2;//顯示的頁碼,當前頁面左右各2,就是5頁 private $pageStart;//顯示頁碼的開始 private $pageEnd;//顯示頁碼的結束 public function __construct($total,$size,$currentPage){ $this->total = $total; $this->size = $size; $this->pageCount=ceil($total/$size); $this->currentPage=$currentPage; $this->pageStart = $this->currentPage-$this->showPages; $this->pageEnd = $this->currentPage+$this->showPages; //如果起始頁碼小於1那麽就設定為1 if($this->pageStart<=1){ //難點 $this->pageEnd = $this->pageEnd+(1-$this->pageStart); $this->pageStart=1; } //如果結束頁大於總頁數,重置結束頁=總頁數 if($this->pageEnd>=$this->pageCount){ //難點 if($this->pageCount<$this->showPages*2+1){ $this->pageStart=1; }else{ $this->pageStart=$this->pageStart-($this->pageEnd-$this->pageCount); } $this->pageEnd=$this->pageCount; } } public function Pages(){ $str=‘<div id=page>‘; $str.= $this->home(); $str.= $this->lastPage(); $str.= $this->numPage(); $str.= $this->nextPage(); $str.= $this->last(); $str.= $this->countTotal(); $str.=‘</div>‘; return $str; } //首頁 private function home(){ if($this->currentPage==1){ $str = ‘<p>首頁</p>‘; }else{ $str = ‘<a href=?page=1>首頁</a>‘; } return $str; } //上一頁 private function lastPage(){ if($this->currentPage==1){ $str = ‘<p>上一頁</p>‘; }else{ $str = ‘<a href=?page=‘.($this->currentPage-1).‘>上一頁</a>‘; } return $str; } //中間的數字頁 private function numPage(){ $str=‘<p class="pageRemark">... </p> ‘; for($i=$this->pageStart;$i<=$this->pageEnd;$i++){ if($i==$this->currentPage){ $str.=‘<a class="cur" href=?page=‘.$i.‘>‘.$i.‘</a>‘; }else{ $str.=‘<a href=?page=‘.$i.‘>‘.$i.‘</a>‘; } } $str.=‘<p class="pageRemark">...</p> ‘; return $str; } //下一頁 private function nextPage(){ if($this->currentPage==$this->pageCount){ $str = ‘<p>下一頁</p>‘; }else{ $str = ‘<a href=?page=‘.($this->currentPage+1).‘>下一頁</a>‘; } return $str; } //末頁 private function last(){ if($this->currentPage==$this->pageCount){ $str = ‘<p>末頁</p>‘; }else{ $str = ‘<a href=?page=‘.$this->pageCount.‘>末頁</a>‘; } return $str; } //統計 private function countTotal(){ return ‘<p class="pageRemark">共<b>13</b>頁<b>100</b>條數據</p>‘; } }
調用 page.php
<?php //引入分頁類 require_once ‘pageClass.php‘; //鏈接數據庫 try{ $db = new PDO(‘mysql:host=127.0.0.1;dbname=shop;charset=utf8;port=3306‘,‘root‘,‘root‘); }catch(PDOException $e){ echo ‘mysql error:‘.$e->getMessage(); } $sql = ‘select * from student‘; $stmt = $db->prepare($sql); $stmt->execute(); //獲取總條數 $total = $stmt->rowCount(); $size = 5;//每頁條數 0,8; 8,8 16,8 //如果不存在第一頁,則默認第一頁 isset($_GET[‘page‘])?$currentPage = $_GET[‘page‘]:$currentPage=1; $sql.=" limit ".($currentPage-1)*$size.",$size"; $stmt = $db->prepare($sql); $stmt->execute(); //查詢結果集 $res = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> p{margin:0} #page{height:40px;padding:20px 0px;} #page a{display:block;float:left;margin-right:10px;padding:2px 12px;height:24px;border:1px #cccccc solid;background:#fff;text-decoration:none;color:#808080;font-size:12px;line-height:24px;} #page a:hover{color:#077ee3; border:1px #077ee3 solid;} #page a.cur{border:none;background:#43badb;color:#fff;} #page p{float:left;padding:2px 12px;font-size:12px;height:24px;line-height:24px;color:#bbb;border:1px #ccc solid;background:#fcfcfc;margin-right:8px;} #page p.pageRemark{border-style:none;background:none;margin-right:0px;padding:4px 0px;color:#666;} #page p.pageRemark b{color:red;} #page p.pageEllipsis{border-style:none;background:none;padding:4px 0px;color:#808080;} </style> </head> <body> <?php echo ‘<table>‘; echo ‘<tr><th>ID</th><th>姓名</th><th>Emai</th><th>手機號</th></tr>‘; foreach($res as $k=>$v){ echo ‘<tr><td>‘.$v[‘id‘].‘</td><td>‘.$v[‘name‘].‘</td><td>‘.$v[‘email‘].‘</td><td>‘.$v[‘mobile‘].‘</td></tr>‘; } echo ‘</table>‘; $page = new Page($total,$size,$currentPage); echo $page->Pages(); ?> </body> </html>
PHP之簡單的分頁類