分页程序设计理解page.class.php

WBOY
Release: 2016-06-23 14:37:47
Original
1609 people have browsed it

关于分页程序,我的理解是当点击某个链接如:[下一页]就显示下一页对应的数据.

这本质是对数据库的操作,简而言之换成SQL语句就是 select some_fields from table limit Offset Num;

这里Num是固定的,比如每页显示10条数据

关键是Offset,即偏移量,每点击下一页时候,这个值就会发生变化;如:0,10     10,10     20,10....

在页面上最终显示的只有两个东西,一个是对应的数据(图片,文字等...),一个是 分页链接(共xx条记录,每页显示10条,当前第1/32页 [首页] [上页] [下页] [尾页] )

下面具体实现代码

<?phpclass page{    //总记录数量    public $count;    //每页显示多少条    public $meiye;    //总页数    public $pages;    //当前是第几页    public $now_page;    //上一页    private $prev;    //下一页    private $next;    //偏移量    public $offset;    //样式    public $style;    //请求文件的地址    private $url;    //传入4个参数:1总记录条数,2每页显示条数,3执行脚本的文件名,4输出样式(可选)    public function __construct($count,$meiye,$url,$style=1){        $this->count=$count;         $this->meiye=$meiye;        $this->pages=ceil($this->count/$this->meiye);        $this->now_page=$this->get_now_page();        $this->url=$url;        $this->prev=$this->get_prev_page();        $this->next=$this->get_next_page();        $this->offset=$this->get_offset();        $this->style=$this->get_style($style);    }    //获得当前是第几页    private function get_now_page(){        return isset($_GET['page'])?$_GET['page']:1;    }    //获得上一页    private function get_prev_page(){        return $this->now_page-1?$this->now_page-1:false;    }    //获得下一页    private function get_next_page(){        return $this->now_page+1>$this->pages?false:$this->now_page+1;    }    //获得偏移量    private function get_offset(){        return $this->now_page<=1&&$this->now_page>0?0:($this->now_page-1)*($this->meiye);    }    //获得分页链接风格默认是:    //共30条记录,每页显示4条,当前第1/8页 [首页] [下一页] [尾页]    private function get_style($s){        $str='';        if ($this->pages>1) {            if($s==1){                $str.="共{$this->count}条记录,每页显示{$this->meiye}条,当前第{$this->now_page}/{$this->pages}页 ";                $str.="<a href='$this->url?page=1'>[首页]</a> ";                $this->prev?$str.="<a href='$this->url?page=$this->prev'>[上一页]</a> ":false;                 $this->next?$str.="<a href='$this->url?page=$this->next'>[下一页]</a> ":false;                 $str.="<a href='$this->url?page=$this->pages'>[尾页]</a>";             }else{                $str.= "当前第{$this->now_page}/{$this->pages}页";                $str.="<a href='$this->url?page=1'>[首页]</a> ";                $this->prev?$str.="<a href='$this->url?page=$this->prev'>[上一页]</a> ":false;                 for ($j = 1; $j <= $this->pages; $j++) {                    $str.="<a href=$this->url?page=$j>$j</a>  ";                }                $this->next?$str.="<a href='$this->url?page=$this->next'>[下一页]</a> ":false;                 $str.="<a href='$this->url?page=$this->pages'>[尾页]</a>";             }            return $str;        }    }}
Copy after login

当使用的时候,取出偏移量Offset,和style

<?php    include 'mysql.class.php';    include 'page.class.php';    //实例化模型    $m=new Model('woliu');    //获得表中的所有数据    $c=$m->acount();    //实例化分页类(总条数,每页显示多少条记录,执行脚本,样式)    $p=new page($c,4,'page3.php',1);    //获得偏移量    $of=$p->offset;    //查询数据库    $res=$m->select('pic','','',"$of,4");    //便利结果集$res    //输出分页链接    echo $p->style;
Copy after login

 

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template