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->per_page_rows=$per_page_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.=" uri}&page=1'>{$this->config['first']} ";
- }
- return $html;
- }
- private function go_prev() {
- $html="";
- if ($this->page==1) {
- $html.=" {$this->config['prev']} ";
- }else{
- $html.=" uri}&page={$this->page}-1'>{$this->config['prev']} ";
- }
- return $html;
- }
- private function go_next() {
- $html="";
- if ($this->page==$this->total_pages) {
- $html.=" {$this->config['next']} ";
- }else{
- $html.=" uri}&page={$this->page}+1'>{$this->config['next']} ";
- }
- return $html;
- }
- private function go_last() {
- $html="";
- if ($this->page==$this->total_pages) {
- $html.=" {$this->config['last']} ";
- }else{
- $html.=" uri}&page={$this->total_pages}'>{$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<1) {
- continue;
- }else{
- $link_page.=" uri}&page={$page}'>{$page} ";
- }
- }
- $link_page.=" {$this->page} ";
- for ($i = 1; $i < $i_num; $i++) {
- $page=$this->page+$i;
- if ($page<=$this->total_pages) {
- $link_page.=" uri}&page={$page}'>{$page} ";
- }else{
- break;
- }
- }
- return $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->page}/{$this->total_pages}頁 ";
- $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 Page($total_rows,$per_page_rows);
- $sql="select * from cp_sd_day {$page->limit}";
- $rt=mysql_query($sql);
- echo '
- echo '
- while (!!$row=mysql_fetch_assoc($rt)) {
- echo '
- echo '
- echo '
- echo '
- echo '
- echo '
- }
- echo '
';
cp_sd_day
';
- echo '
';
| '.$row['date_no'].' | ';
'.$row['max_notwin'].' | ';
'.$row['sum_last_miss'].' | ';
'.$row['last_miss'].' | ';
'.$row['last_miss_sort'].' | ';
';
- echo '
| '.$page->out_page(array(2,3,4,5,6,7,8)).' |
';
';
- ?>
複製代碼
|