PHP 自製分頁類

來源:互聯網
上載者:User

標籤:query   soc   顯示   for   etc   擷取   sql   iss   root   

思路:

通過給頁面url傳遞get參數,來控制每頁的sql查詢(mysql關鍵詞:limit),實現分頁查詢

代碼:

 

 1 class getpage{ 2      3     public $pagenum; 4     public $pagecontents; 5     public $sql; 6     public $url;  7     public function __construct($a){ 8          9         $this->pagecontents = $a;10         $this->url = ‘http://‘.$_SERVER[‘HTTP_HOST‘].$_SERVER[‘REQUEST_URI‘];11     }12     //擷取分頁數13     public function createpage($sql){14         15         $this->sql = $sql;16         $conn = mysql_connect(‘localhost‘,‘root‘,‘root‘);17         if(!$conn){18             19             die(mysql_error());20         }21         mysql_select_db(‘class‘,$conn)or die(mysql_error());22         mysql_query("set names ‘utf8‘");23         $handle = mysql_query($sql);24         $row = array();25         while($r = mysql_fetch_row($handle)){26             27             $row[] = $r;28         }29         //假如除的結果為小數,則進1來保證分頁數量30         $pagenum = count($row)/$this->pagecontents;31         $this->pagenum = ceil($pagenum);32         33     }34     //替換page參數35     private function replacestr(){36         if(isset($_GET[‘page‘])){37             return str_replace("?page="[email protected]$_GET[‘page‘],"", $this->url);38         }else{39             @$_GET[‘page‘] = 0;40         }41     }42     //頁面分頁顯示43     public function createstyle(){44         45         for($i=0;$i<$this->pagenum;$i++){46             47             echo "<button><a href=‘".$this->replacestr($this->url)."?page=".$i*$this->pagecontents."‘>".(1+$i)."</a></button>";48         }49     }50     public function getdata(){51         52         $where = " limit ".(@$_GET[‘page‘]).",".$this->pagecontents;53         $this->sql .= $where; 54         $handle = mysql_query($this->sql);55         $arr = array();56         while($r = mysql_fetch_assoc($handle)){57             $arr[] = $r;58         }59         return $arr;60     }61     62 }

 

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.