码迷,mamicode.com
首页 > Web开发 > 详细

php无限分页类

时间:2015-01-23 07:15:24      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

搞了2个小时,哈哈,自己按照自己的想法把他写出来了!虽然效率有点慢,有些验证的东西也忽略了!

page.class.php

  1 <?php
  2 /*
  3 *    
  4 *    
  5 *    
  6 */
  7 class Pages{
  8     public $rows;//每页显示的行数
  9     public $sqli;//sqli对象
 10     private $result;//结果集对象
 11     private $sql_str;//查询字符串
 12     public $total_rows;//总的条数
 13     public $total_pages;//总的页数
 14     private $field_str = ‘‘;//字段的显示
 15     private $bottom_str;//表格底部信息
 16     private $field_num;//字段数目
 17     private $cur_page;//当前页面
 18     private $next_page;//下一页
 19     private $pre_page;//上一页
 20     private $next_link;//下一页链接
 21     private $pre_link;//上一页链接
 22     private $cur_text;//当前页码
 23     private $result_str = ‘‘;//得到结果集字符串
 24     private $tb;//需要查询的表格
 25     public function __construct($rows,$total_rows,$sqli,$db,$tb){
 26         $this->rows = $rows<=$total_rows?$rows:$total_rows;
 27         $this->sqli = $sqli;
 28         $this->total_rows = $total_rows;
 29         $this->tb = $tb;
 30         $this->sqli->select_db($db);
 31         $this->setTotalPages();
 32         $this->setPage();
 33         $this->setSqlStr();
 34         $this->setResult();
 35         $this->setFieldStr();
 36         $this->setFieldNum();
 37         $this->setLink();
 38         $this->setBottomStr();
 39         $this->setResultStr();
 40     }
 41     private function setTotalPages(){
 42         $this->total_pages = ceil($this->total_rows/$this->rows);
 43     }
 44     private function setFieldStr(){
 45         $this->field_str .= ‘<tr style="color:red;">‘;
 46         while($field_name = $this->result->fetch_field()){
 47             $this->field_str .= ‘<th>‘.$field_name->name.‘</th>‘;
 48         }
 49         $this->field_str .= ‘</tr>‘;
 50     }
 51     private function setFieldNum(){
 52         $this->field_num = $this->result->field_count;
 53     }
 54     private function setBottomStr(){
 55         $this->bottom_str ="
 56         <tr style=‘color:blue;‘>
 57         <th colspan=‘{$this->field_num}‘>
 58         Total rows: {$this->total_rows}&nbsp;&nbsp;&nbsp;Total pages: {$this->total_pages}&nbsp;&nbsp;&nbsp;Per: {$this->rows} Rows/Page
 59         
 60         </th>
 61         </tr>";
 62     }
 63     private function setSqlStr(){
 64         $start = ($this->cur_page-1)*$this->rows;
 65         $this->sql_str = "SELECT * FROM {$this->tb} LIMIT {$start},{$this->rows}";
 66     }
 67     private function setResult(){
 68         $this->result = $this->sqli->query($this->sql_str);
 69     }
 70     private function setPage(){
 71         $this->cur_page = isset($_GET[‘page‘])?$_GET[‘page‘]:1;
 72         $this->validatePage();
 73         $np = $this->cur_page + 1;
 74         $pp = $this->cur_page - 1;
 75         $this->next_page = ($np > $this->total_pages)? $this->total_pages+1 : $np;
 76         $this->pre_page = $pp;
 77     }
 78     private function setLink(){
 79         $link_span = floor($this->field_num / 3);
 80         $cur_span = $this->field_num - ($link_span * 2);
 81         if($this->next_page >= ($this->total_pages+1)){
 82             $this->next_link = "<th colspan=‘{$link_span}‘><span>MAX</span></th>";
 83         }else{
 84             $this->next_link = "<th colspan=‘{$link_span}‘><a href=‘?page={$this->next_page}‘>Next</a></th>";
 85         }
 86         if($this->pre_page == 0){
 87             $this->pre_link = "<th colspan=‘{$link_span}‘><span>MIN</span></th>";
 88         }else{
 89             $this->pre_link = "<th colspan=‘{$link_span}‘><a href=‘?page={$this->pre_page}‘>Previous</a></th>";
 90         }
 91         $this->cur_text = "<th colspan=‘{$cur_span}‘>Page:{$this->cur_page}</th>";
 92     }
 93     //验证页码的有效性
 94     private function validatePage(){
 95         if($this->cur_page <= 1){
 96             $this->cur_page = 1;
 97         }
 98         if($this->cur_page >= $this->total_pages){
 99             $this->cur_page = $this->total_pages;    
100         }
101     }
102     private function setResultStr(){
103         $j=0;
104         while($results = $this->result->fetch_array(MYSQLI_NUM)){
105             if($j%2==0){
106                 $this->result_str .= ‘<tr style="background:#EEE;">‘;
107             }else{
108                 $this->result_str .= ‘<tr>‘;
109             }
110             for($i=0; $i<$this->field_num;$i++){
111                 $this->result_str .= "<th>{$results[$i]}</th>";
112             }
113             $this->result_str .= ‘</tr>‘;
114             $j++;
115         }
116     }
117     public function view(){
118         $view_str = "<table style=‘border:1px solid black;‘>".$this->field_str.$this->result_str.$this->pre_link.
119         $this->cur_text.$this->next_link.$this->bottom_str."</table>";
120         echo $view_str;
121     }
122     public function __destruct(){
123         $this->result->free();
124         $this->sqli->close();
125     }
126 }

page.php

 1 <style type="text/css">
 2 th{
 3     width:200px;
 4     border:1px solid #ccc;
 5 }
 6 table{
 7     margin:0px auto;
 8 }
 9 a{
10     text-decoration:none;
11 }
12 </style>
13 <?php
14 echo ‘<pre>‘;
15 include_once(‘page.class.php‘);
16 $rows = 8;
17 $total_rows = 40;
18 $tb = ‘CHARACTER_SETS‘;
19 $db = ‘information_schema‘;
20 $sqli = new mysqli(‘localhost‘,‘root‘,‘lf1234‘);
21 $page = new Pages($rows,$total_rows,$sqli,$db,$tb);
22 $page->view();

技术分享

php无限分页类

标签:

原文地址:http://www.cnblogs.com/coolphper/p/4243164.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!