php之分頁類代碼_PHP教程

來源:互聯網
上載者:User
[php] /* 思路 1.把地址欄的URL擷取 2.分析URL中的query部分--就是?後面傳參數的部分 3.query部分分析成數組 4.把數組中的page單元,+1,-1,形成2個新的數組 5.再把新數組拼接成query部分,合成上一頁,下一頁串連地址 */ //分頁類 class Page { public $total; //全部條數,從資料庫取出 public $prePage = 10; //每頁的條數 protected $curr= 1; //預設當前頁碼 public function __construct($total,$prePage='') { $this->total = $total; //把總條目資訊放在total屬性 if ($prePage > 0) { $this->prePage = $prePage; //把每頁數量放在perPage屬性 } //計算當前頁碼 if (isset($_GET['page']) && ($_GET['page'] + 0) > 0) { $this->curr = $_GET['page'] + 0; } } //主體函數 public function showPage() { if ($this->total <=0) { return ''; //如果總條目<=0 直接返回Null 字元串 } $cnt = ceil($this->total / $this->prePage); //算總頁數,進一取整 //根據當前頁,算上一頁,下一頁 /* 分析url,有幾種情況? xx.php xx.php?id=5 xx.php?page=3 xx.php?id=5&page=3 */ //最終產生的URL裡邊必然有page=N $url = $_SERVER['REQUEST_URI']; $parse = parse_url($url); //把URL分析結果放在數組裡 //print_r($parse); //保證參數裡邊有page if (!isset($parse['query'])) { $parse['query'] = 'page=' .$this->curr; } //把query字串分析成數組,再次確保有page選項 parse_str($parse['query'],$parms); if (!array_key_exists('page', $parms)) { $parms['page'] = $this->curr; } //上邊四種情況都測試一遍,page參數都能產生 //print_r($parms); //判斷除了page之外,還有沒有其他參數 if (count($parms) == 1) { $url = $parse['path'] . '?'; } else { unset($parms['page']); $url = $parse['path'] . '?' . http_build_query($parms) . '&'; } //echo $url $prev = $this->curr - 1; $next = $this->curr + 1; //首頁 $indexLink = '首頁'; //上一頁 if ($prev < 1) { $prevLink = ''; }else { $prevLink = '上一頁'; } //下一頁 if ($next > $cnt) { $nextLink = ''; }else { $nextLink = '下一頁'; } //尾頁 $lastLink = '尾頁'; //echo $indexLink.' '.$prevLink.' '.$nextLink .' '.$lastLink; //上一頁,1 2 3 4 5 下一頁 $start = $this->curr - (5-1)/2; //計算左側開始的頁碼 $end = $this->curr + (5-1)/2; //計算右側開始的頁碼 //如果左側的頁面,已經小於1,則把小於1 的部分補到右側 if ($start < 1) { $end += (1 - $start); $start = 1; //修改start = 1 if ($end > $cnt) { $end = $cnt; } } //把右側超出的部分,補到左邊 if ($end > $cnt) { $start -= ($end - $cnt); $end = $cnt; if ($start < 1) { $start = 1; } } //迴圈出頁碼數 $pageStr = ''; for ($i=$start; $i <= $end ; $i++) { if ($i == $this->curr) { $pageStr .= $i; continue; } $pageStr .= '' . $i . ''; } return $indexLink.$prevLink.$pageStr.$nextLink.$lastLink; } } $page = new Page(30,3); echo $page->showPage();

http://www.bkjia.com/PHPjc/477842.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477842.htmlTechArticle[php] /* 思路 1.把地址欄的URL擷取 2.分析URL中的query部分--就是?後面傳參數的部分 3.query部分分析成數組 4.把數組中的page單元,+1,-1,形成2個...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.