PHP分頁類的使用
page.class.php
-
class Page {
- private $total_rows;//資料庫總條數
- private $ per_page_rows;//每頁顯示條數
- private $limit;
- private $uri;
- private $total_pages;//總頁數
- private $config=array("header"=>"記錄條數","prev"=>"上一頁","next"=>"下一頁","first"=>"首頁","last"=>"尾頁");
- private $list_length=8;
- public function __construct($total_rows,$per_page_rows=10,$url_args){
- $this->total_rows=$total_rows; $this->uri=$this->get_uri($url_args);
- $this->page = !empty($_GET['page']) ? $_GET['page'] : 1;
- $this->total_pages=ceil($this->total_rows/$this->per_page_rows);
- $this->limit=$this->set_limit();
- }
- private function set_limit () {
- return "limit ".($this->page-1)*$this->per_page_rows.",{$this->per_page_rows}";
- }
- private function get_uri($ url_args) {
- $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?") ? "" : "?").$url_args;
- $parse= parse_url($url);
- if (isset($parse['query'])) {
- parse_str($parse['query'],$params);//把url字串解析為陣列
- unset($params['page']);//刪除數組下標為page的值
- $url=$parse['path'].'?'.http_build_query($params);//再次建構url
- }
- return $url;
- }
- public function __get($args) {
- if ($args=="limit") {
- return $this-> limit;
- }else{
- return null;
- }
- }
- private function start_page(){
- if ($this->total_rows==0) {
- return 0;
- }else{
- return (($this->page-1)*$this->per_page_rows)+1;
- }
- }
- private function end_page(){
- return min($this->page*$this->per_page_rows,$this->total_rows);
- }
- private function go_first() {
- $html="";
- if ($this->page==1) {
- $html.=" {$this->config['first']} ";
- }else{
- $html.=" {$this->config['first']} ";
- }
- return $html;
- }
- private function go_prev() {
- $html="";
- if ($this->page==1) {
- $html.=" {$this->config['prev']} ";
- }else{
- $html.=" {$this->config['prev ']} ";
- }
- return $html;
- }
- private function go_next() {
- $html="";
- if ($this- >page==$this->total_pages) {
- $html.=" {$this->config['next']} ";
- }else{
- $html.=" {$this->config['next']} ";
- }
- return $html ;
- }
- private function go_last() {
- $html="";
- if ($this->page==$this->total_pages) {
- $html.=" {$this->config['last']} ";
- }else{
- $html.=" {$this->config['last']} ";
- }
- return $html;
- }
- private function go_page() {
- return ' ';
- }
- private function page_list() {
- $link_page="";
- $i_num=floor($this->list_length/2);
- for ($i = $i_num; $i >= 1; $i--) {
- $page=$this->page-$ i;
- if ($page continue;
- }else{
- $link_page.=" {$page} ";
- }
- }
- $link_page.=" {$this->page} ";
- for ($i = 1; $i $page=$this- >page+$i;
- if ($pagetotal_pages) {
- $link_page.=" {$page} ";
- }else{
- 規則;
- }
- }
- 回傳$link_page ;
- }
- public function out_page($display= array(0,1,2,3,4,5,6,7,8)) {
- $display_html='';
- $ html[0]=" 共有{$this-> Total_rows}{$this->config['header']} ";
- $html[1 ]=" 每頁顯示".($this->end_page()-$this- >start_page()+1)."條,本頁顯示從{$this->start_page()}--{$this->end_page()}{$this->config['header'] } ";
- $html[2]=" {$this->頁}/{$this-> Total_pages} b>頁";
- $html[3]=$this->go_first();
- $html[4]=$this->go_prev();
- $html[5 ]=$this->page_list();
- $html[6]=$this->go_next();
- $html[7]=$this->go_last();
- $html[ 8]=$this->go_page();
- foreach ($display as $index){
- $display_html.=$html[$index];
- }
- return $display_html;
- }
- }
- ?>
複製程式碼
page_demo.php
-
-
-
-
-
-
-
-
-
-
-
-
-
header("content-type:text/html;charset=utf-8");
- require_once './page.class.php';
- require_once '../config/config.db.php';
- //資料庫中的總條數:total_rows;
- //每一頁顯示的條數:per_page_rows
- $sql ="select * from cp_sd_day";
- $rt=mysql_query($sql);
- $total_rows=mysql_num_rows($rt);
- $per_page_rows=10;
-
- $page=new頁($total_rows,$per_page_rows);
- $sql="select * from cp_sd_day {$page->limit}";
- $rt=mysql_query($sql);
- echo '
';- echo '
cp_sd_day'; while (!!$row=mysql_fetch_assoc ($rt)) { echo ''; echo ''.$row['date_no'].' | '; echo ''.$row[' max_notwin '].' | '; echo ''.$row['sum_last_miss'].' | '; echo '
'.$row[' last_miss '].' | '; echo ''.$row['last_miss_sort'].' | ; '; echo ' '; } echo ''.$page->out_page ( 備份(2, 3,4,5,6,7,8)).' | ';
echo '
|