PHP實現可自訂樣式的分頁類_php技巧

來源:互聯網
上載者:User

本文執行個體為大家分享了PHP實現可自訂樣式的分頁類,供大家參考,具體內容如下

<?php //namespace Component;/** * 2016-3-27 * @author ankang */class Page { private $ShowPage; private $CountPage; private $Floorp; private $PageUrl; private $PageClass; private $CurClass;  /** * @author ankang * @param number $CountNum  資料總數 * @param string $PageUrl  跳轉連結 * @param string $PageClass  <a>標籤 總體樣式  * @param string $PageUrl  當前頁樣式 * @param number $PageSize  每頁顯示的資料條數 * @param number $ShowPage  每次顯示的頁數  */ public function __construct($CountNum, $PageUrl = NULL, $PageClass = NULL,$CurClass = NULL, $PageSize = 20, $ShowPage = 5) { $this->ShowPage = $ShowPage; $this->CountPage  = ceil ( $CountNum / $PageSize ); $this->Floorp  = floor ( $ShowPage / 2 ); // 位移量  $this->PageClass  = is_null ( $PageClass ) ? '' : $PageClass; $this->CurClass = is_null ( $CurClass ) ? '' : $CurClass;   // $ServerURL  = ( preg_match('/\?/i', $_SERVER['REQUEST_URI']))?preg_replace('/\&p\=[0-9]+/i', "", $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']."?"; // if( substr($ButURL,0,2)=='//' ){  // $ServerURL  = substr($ServerURL,1); // } // $url   = preg_replace('/p=[\d]*/i', '', $ServerURL);  $url   = ''; //推薦自己傳url,不傳也可以開啟上面的代碼自動擷取 $this->PageUrl  = is_null ( $PageUrl ) ? $url : $PageUrl; }  /** * * @param number $Page   * @param string $ShowToPage *  首頁,上下頁,尾頁 * @param string $Html 標籤元素,li,p  * @return string */ public function getPage($Page = 1, $ShowToPage = true, $Html = null) { $StartPage  = ($Page - $this->Floorp); // 開始頁碼 $EndPage  = ($Page + $this->Floorp); // 結束頁碼   if ($this->CountPage < $this->ShowPage) {  $StartPage = 1;  $EndPage = $this->CountPage; }   if ($StartPage < 1) {  $StartPage = 1;  $EndPage = $this->ShowPage; }   if ($EndPage > $this->CountPage) {  $StartPage = $this->CountPage - $this->ShowPage + 1;  $EndPage = $this->CountPage; }   $PageHtml = '';   if (! is_null ( $Html )) {  if ($Html == 'li') {  $Shtml = '<li>';  $Ehtml = '</li>';  } else {  $Shtml = '<p>';  $Ehtml = '</p>';  } }   if (true == $ShowToPage) {  $PageHtml  .= "$Shtml<a href='{$this->PageUrl}p=1'>« 首頁</a>$Ehtml";  $PrveUrl   = $this->getPrve($Page);  $PageHtml  .= "$Shtml<a href='{$PrveUrl}'>« 上一頁</a>$Ehtml"; }   for($i = $StartPage; $i <= $EndPage; $i ++) {  if ($Page == $i) {  $PageHtml  .= "$Shtml<a href='{$this->PageUrl}p={$i}' class='{$this->CurClass}'>{$i}</a>$Ehtml";  } else {  $PageHtml  .= "$Shtml<a href='{$this->PageUrl}p={$i}' class='{$this->PageClass}'>{$i}</a>$Ehtml";  } }   if (true == $ShowToPage) {  $NextUrl   = $this->getNext($Page);  $PageHtml  .= "$Shtml<a href='{$NextUrl}'>下一頁 »</a>$Ehtml";  $PageHtml  .= "$Shtml<a href='{$this->PageUrl}p={$this->CountPage}' >尾頁 »</a>$Ehtml"; }   return $PageHtml; }  public function getPrve($Page){ if ($Page != 1) {  $Prve  = $Page - 1;  $PrveUrl  = "{$this->PageUrl}p={$Prve}"; } else {  $PrveUrl  = "{$this->PageUrl}p=1"; }   return $PrveUrl; }  public function getNext($Page){ if ($Page != $this->CountPage) {  $Next  = $Page + 1;  $NextUrl  = "{$this->PageUrl}p={$Next}"; } else {  $NextUrl  = "{$this->PageUrl}p={$this->CountPage}"; }   return $NextUrl; }   }

再為大家分享一個主要用於新手學習php分頁,代碼簡單實用,主要是注釋很完整。

1. Page.class.php

<?php/** * 分頁類 *  * 調用方式: * $p=new Page(總頁數,顯示頁數,當前頁碼,每頁顯示條數,[連結]); * print_r($p->getPages()); //產生一個頁碼數組(鍵為頁碼,值為連結) * echo $p->showPages(1); //產生一個頁碼樣式(可添加自訂樣式) *  * @author: Dzer <Email:358654744@qq.com Blog:Dzer.me> * @version: 2014-12-25 09:09:42 * @Last Modified time: 2014-12-28 17:37:13 */ /*思路:給我一個 總頁數,需要顯示的頁數,當前頁,每頁顯示的條數,串連寫一個方法 產生一個一維數組,鍵為頁碼 值為串連寫一個方法 返回一個產生好樣式的頁碼(並且可以根據自己需要添加樣式)預設樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]*/class Page{ protected $count;  //總條數 protected $showPages; //需要顯示的頁數 protected $countPages; //總頁數 protected $currPage; //當前頁 protected $subPages; //每頁顯示條數 protected $href;  //串連 protected $page_arr=array(); //儲存產生的頁碼 鍵頁碼 值為串連  /**  * __construct 建構函式(擷取分頁所需參數)  * @param int $count  總條數  * @param int $showPages 顯示頁數  * @param int $currPage 當前頁數  * @param int $subPages 每頁顯示數量  * @param string $href 串連(不設定則擷取當前URL)  */ public function __construct($count,$showPages,$currPage,$subPages,$href=''){  $this->count=$count;  $this->showPages=$showPages;  $this->currPage=$currPage;  $this->subPages=$subPages;     //如果連結沒有設定則擷取當前串連  if(empty($href)){   $this->href=htmlentities($_SERVER['PHP_SELF']);   }else{   $this->href=$href;  }  $this->construct_Pages(); }  /**  * getPages 返回頁碼數組  * @return array 一維數組 鍵為頁碼 值為連結  */ public function getPages(){  return $this->page_arr; }  /**  * showPages 返回產生好的頁碼  * @param int $style 樣式  * @return string  產生好的頁碼  */ public function showPages($style=1){  $func='pageStyle'.$style;  return $this->$func(); }  /**  * pageStyle1 分頁樣式(可參照這個添加自訂樣式 例如pageStyle2())  * 樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]   * @return string   */ protected function pageStyle1(){  /* 構造普通模式的分頁   共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]   */  $pageStr='共'.$this->count.'條記錄,每頁顯示'.$this->subPages.'條';  $pageStr.='當前第'.$this->currPage.'/'.$this->countPages.'頁 ';   $_GET['page'] = 1;  $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">首頁</a>] </span>';  //如果當前頁不是第一頁就顯示上頁  if($this->currPage>1){   $_GET['page'] = $this->currPage-1;   $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">上頁</a>] </span>';  }   foreach ($this->page_arr as $k => $v) {   $_GET['page'] = $k;   $pageStr.='<span>[<a href="'.$v.'">'.$k.'</a>] </span>';  }   //如果當前頁小於總頁數就顯示下一頁  if($this->currPage<$this->countPages){   $_GET['page'] = $this->currPage+1;   $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">下頁</a>] </span>';  }   $_GET['page'] = $this->countPages;  $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'">尾頁</a>] </span>';   return $pageStr; }  /**  * construct_Pages 產生頁碼數組  * 鍵為頁碼,值為連結  * $this->page_arr=Array(  *     [1] => index.php?page=1  *     [2] => index.php?page=2  *     [3] => index.php?page=3  *     ......)  */ protected function construct_Pages(){  //計算總頁數  $this->countPages=ceil($this->count/$this->subPages);  //根據當前頁計算前後頁數  $leftPage_num=floor($this->showPages/2);  $rightPage_num=$this->showPages-$leftPage_num;   //左邊顯示數為當前頁減左邊該顯示的數 例如總顯示7頁 當前頁是5 左邊最小為5-3 右邊為5+3  $left=$this->currPage-$leftPage_num;  $left=max($left,1); //左邊最小不能小於1  $right=$left+$this->showPages-1; //左邊加顯示頁數減1就是右邊顯示數  $right=min($right,$this->countPages); //右邊最大不能大於總頁數  $left=max($right-$this->showPages+1,1); //確定右邊再計算左邊,必須二次計算     for ($i=$left; $i <= $right; $i++) {   $_GET['page'] = $i;   $this->page_arr[$i]=$this->href.'?'.http_build_query($_GET);  } }}

2. demo.php 

<?php/** * 分頁類demo * Be the best of whatever you are! *  * @author: Dzer<358654744@qq.com> * @version: 2014-12-28 17:38:23 * @Last Modified time: 2014-12-28 18:08:28 */header("content-type:text/html;charset=utf8");include('./Page.class.php'); //引入類 //$p=new Page(總頁數,顯示頁數,當前頁碼,每頁顯示條數,[連結]);//串連不設定則為當前連結$page=isset($_GET['page']) ? $_GET['page'] : 1;$p=new Page(100,7,$page,8); //產生一個頁碼數組(鍵為頁碼,值為連結)echo "<pre>";print_r($p->getPages());  //產生一個頁碼樣式(可添加自訂樣式)//樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]echo $p->showPages(1); 

以上就是本文的全部內容,希望對大家學習PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.