您好,登錄后才能下訂單哦!
寫了兩個版本,還是后面好點,只計算關鍵數據,返回數組,html代碼例外拼接
<?php /** * 分頁類 * @author timo * @version 2016-12-11 08:15 */ class paginate{ //當前頁碼 private $page; //記錄總條目 private $entry_total; //每頁顯示多少個記錄 private $page_size; //純數值li總數,即分頁欄的長度(不包括首尾上下頁) private $page_li_count; //總頁數 private $page_count; private $result; private $active; //該url末尾必需帶有?或者& private $url; /** * * @param unknown $url 跳轉的鏈接地址 * @param unknown $entry_total 記錄的總條目 * @param number $page_size 每頁顯示多少個記錄 * @param number $page_li_count 分頁欄顯示多少個純數值的li (不包括首尾上下頁),即分頁欄的長度 */ public function __construct($entry_total,$page_size = 5,$page_li_count = 1){ //初始化成員變量,參數還需要仔細篩選 $this->entry_total = (int)$entry_total; $this->page_size = (int)$page_size; $this->page_count = ceil($entry_total/$page_size); //分頁欄的長度取用戶提交的分頁欄長度與頁面總數中的較小值 $this->page_li_count = $page_li_count > $this->page_count ? $this->page_count : $page_li_count; } /** * 設置當前頁及active狀態 * @param unknown $page 合法參數為:first end 1234 */ public function setPage($page){ if ($page == 'end') { $this->active = 'end'; $this->page = $this->getPagecount(); }elseif ($page>0 && $page<=$this->page_count) { $this->active = $page; $this->page = $page; }else{ //first或傳遞非法值的會被定到首頁去 $this->active = 'first'; $this->page = 1; } } /** * * @param unknown $url //該url末尾必需帶有?或者& */ public function setURL($url){ $this->url = $url; } //當前頁碼 public function getPage(){ return $this->page; } //共計多少頁 public function getPagecount(){ return $this->page_count; } //共計多少條記錄 public function getEntrycount(){ return $this->entry_total; } /** * 計算當前頁的分頁欄需要的信息 * @return 返回數組,包含拼接分頁關鍵信息 */ public function create(){ //首頁 $first_page= "first"; //計算上頁頁碼,最小為1 $pre_page = ($this->page-1)<=1 ? 1 : $this->page-1; //計算下一頁頁碼,最大為page_count $next_page = ($this->page+1)>=$this->page_count ? $this->page_count : $this->page+1; //尾頁 $end_page= "end"; $nums_page = []; //這個判斷與初始化變量中的判斷重復了,但...安全點 if ($this->page_count >= $this->page_li_count) { //中間li為第幾個,偶數取中偏前的一個 $mid_li = $this->page_li_count%2==0 ? $this->page_li_count/2 : (($this->page_li_count-1)/2)+1; //理想情況下,$mid_li后面li應有的個數 $right_li_num = $this->page_li_count - $mid_li; //理想情況下,$mid_li前面li應有的個數 $left_li_num = $mid_li-1; //計算最右邊的頁碼 if ($this->page <= $left_li_num) { //若當前頁左邊li個數不足以讓其居中,則最右端頁碼等于最右端li的編號 $right_li_page = $this->page_li_count; }else { $right_li_page = ($this->page_count-$this->page) >= $right_li_num ? $this->page+$right_li_num : $this->page_count; } for ($i = 0; $i < $this->page_li_count ; $i++) { //看著可能有點繞,紙上算算就知道了,已知最右邊li的頁碼,li的總數,當前li的編號為$i+1,算當前li對應的頁碼 $n = $right_li_page-$this->page_li_count+$i+1; $nums_page[] = $n; } } //將這幾個拼接起來 $this->result['first'] = $first_page; $this->result['end'] = $end_page; $this->result['pre'] = $pre_page; $this->result['next'] = $next_page; $this->result['nums'] = $nums_page; $this->result['active'] = $this->active; return $this->result; } public function getHTML(){ //待完善........ $url = isset($this->url) ? $this->url : '?'; $first_class = $this->result['active'] == 'first' ? 'active' : ''; $end_class = $this->result['active'] == 'end' ? 'active' : ''; $pre_class = $this->result['active'] == 'pre' ? 'active' : ''; $next_class = $this->result['active'] == 'next' ? 'active' : ''; $first_page_html = <<<HTML <li class="$first_class"><a href="{$url}page={$this->result['first']}">首頁</a></li> HTML; $pre_page_html = <<<HTML <li class="$pre_class"><a href="{$url}page={$this->result['pre']}">上一頁</a></li> HTML; $next_page_html = <<<HTML <li class="$next_class"><a href="{$url}page={$this->result['next']}">下一頁</a></li> HTML; $end_page_html = <<<HTML <li class="$end_class"><a href="{$url}page={$this->result['end']}">尾頁</a></li> HTML; $lis_page_html = ''; foreach ($this->result['nums'] as $v){ $li_class = $this->result['active'] == $v ? 'active' : ''; $lis_page_html .= <<<HTML <li class="$li_class"><a href="{$url}page=$v">$v</a></li> HTML; } $htmlcode = $first_page_html.$pre_page_html.$lis_page_html.$next_page_html.$end_page_html; return $htmlcode; } } /*********** * <?php require 'paginate02.class.php'; $a = new paginate(10,5,4); $a-> setPage( isset($_GET['page'])?$_GET['page']:1); $a->create(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>fenye</title> <link rel="stylesheet" href="css/bootstrap.css"> </head> <body> <ul class="pagination"> <?php echo $a->getHTML()?> </ul> </body> </html> * */
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。