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.=" {$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{
- 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 '
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(array(2,3,4,5,6,7,8)).' | '; - echo '
'; - ?>
复制代码
|