Home >Backend Development >PHP Tutorial >php 分页类 代码问题

php 分页类 代码问题

WBOY
WBOYOriginal
2016-06-06 20:34:471064browse

以下在网上找的例子,实际上无法使用。请帮忙指出问题所在?我纳闷这个类不需要操作数据库,查询哪个表吗?

<code>    class Page {
        private $total;     //总记录
        private $pagesize;  //每页显示多少条
        private $limit;     //limit
        private $page;      //当前页码
        private $pagenum;       //总页码
        private $url;       //地址
        private $bothnum;       //两边保持数字分页的量

    //构造方法初始化
    public function __construct($_total, $_pagesize) {
        $this->total = $_total ? $_total : 1;
        $this->pagesize = $_pagesize;
        $this->pagenum = ceil($this->total / $this->pagesize);
        $this->page = $this->setPage();
        $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
        $this->url = $this->setUrl();
        $this->bothnum = 2;
    }

    //拦截器
    private function __get($_key) {
        return $this->$_key;
    }

    //获取当前页码
    private function setPage() {
        if (!empty($_GET['page'])) {
            if ($_GET['page'] > 0) {
                if ($_GET['page'] > $this->pagenum) {
                    return $this->pagenum;
                } else {
                    return $_GET['page'];
                }
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }

    //获取地址
    private function setUrl() {
        $_url = $_SERVER["REQUEST_URI"];
        $_par = parse_url($_url);
        if (isset($_par['query'])) {
            parse_str($_par['query'],$_query);
            unset($_query['page']);
            $_url = $_par['path'].'?'.http_build_query($_query);
        }
        return $_url;
    }   //数字目录
    private function pageList() {
        for ($i=$this->bothnum;$i>=1;$i--) {
        $_page = $this->page-$i;
        if ($_page url.'&page='.$_page.'">'.$_page.' ';
        }
        $_pagelist .= ' <span class="me">'.$this->page.'</span> ';
        for ($i=1;$ibothnum;$i++) {
            $_page = $this->page+$i;
            if ($_page > $this->pagenum) break;
            $_pagelist .= ' <a href="'.%24this->url.'&page='.%24_page.'">'.$_page.'</a> ';
        }
        return $_pagelist;
    }

    //首页  http://ini.iteye.com/
    private function first() {
        if ($this->page > $this->bothnum+1) {
            return ' <a href="'.%24this->url.'">1</a> ...';
        }
    }

    //上一页
    private function prev() {
        if ($this->page == 1) {
            return '<span class="disabled">上一页</span>';
        }
        return ' <a href="'.%24this->url.'&page='.(%24this->page-1).'">上一页</a> ';
    }

    //下一页
    private function next() {
        if ($this->page == $this->pagenum) {
            return '<span class="disabled">下一页</span>';
        }
        return ' <a href="'.%24this->url.'&page='.(%24this->page+1).'">下一页</a> ';
    }

    //尾页
    private function last() {
        if ($this->pagenum - $this->page > $this->bothnum) {
            return ' ...<a href="'.%24this->url.'&page='.%24this->pagenum.'">'.$this->pagenum.'</a> ';
        }
    }

    //分页信息
    public function showpage() {
        $_page .= $this->first();
        $_page .= $this->pageList();
        $_page .= $this->last();
        $_page .= $this->prev();
        $_page .= $this->next();
        return $_page;
    }
}
?>

</code>

调用方式

<code><?php $_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>
</code>

回复内容:

以下在网上找的例子,实际上无法使用。请帮忙指出问题所在?我纳闷这个类不需要操作数据库,查询哪个表吗?

<code>    class Page {
        private $total;     //总记录
        private $pagesize;  //每页显示多少条
        private $limit;     //limit
        private $page;      //当前页码
        private $pagenum;       //总页码
        private $url;       //地址
        private $bothnum;       //两边保持数字分页的量

    //构造方法初始化
    public function __construct($_total, $_pagesize) {
        $this->total = $_total ? $_total : 1;
        $this->pagesize = $_pagesize;
        $this->pagenum = ceil($this->total / $this->pagesize);
        $this->page = $this->setPage();
        $this->limit = "LIMIT ".($this->page-1)*$this->pagesize.",$this->pagesize";
        $this->url = $this->setUrl();
        $this->bothnum = 2;
    }

    //拦截器
    private function __get($_key) {
        return $this->$_key;
    }

    //获取当前页码
    private function setPage() {
        if (!empty($_GET['page'])) {
            if ($_GET['page'] > 0) {
                if ($_GET['page'] > $this->pagenum) {
                    return $this->pagenum;
                } else {
                    return $_GET['page'];
                }
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }

    //获取地址
    private function setUrl() {
        $_url = $_SERVER["REQUEST_URI"];
        $_par = parse_url($_url);
        if (isset($_par['query'])) {
            parse_str($_par['query'],$_query);
            unset($_query['page']);
            $_url = $_par['path'].'?'.http_build_query($_query);
        }
        return $_url;
    }   //数字目录
    private function pageList() {
        for ($i=$this->bothnum;$i>=1;$i--) {
        $_page = $this->page-$i;
        if ($_page url.'&page='.$_page.'">'.$_page.' ';
        }
        $_pagelist .= ' <span class="me">'.$this->page.'</span> ';
        for ($i=1;$ibothnum;$i++) {
            $_page = $this->page+$i;
            if ($_page > $this->pagenum) break;
            $_pagelist .= ' <a href="'.%24this->url.'&page='.%24_page.'">'.$_page.'</a> ';
        }
        return $_pagelist;
    }

    //首页  http://ini.iteye.com/
    private function first() {
        if ($this->page > $this->bothnum+1) {
            return ' <a href="'.%24this->url.'">1</a> ...';
        }
    }

    //上一页
    private function prev() {
        if ($this->page == 1) {
            return '<span class="disabled">上一页</span>';
        }
        return ' <a href="'.%24this->url.'&page='.(%24this->page-1).'">上一页</a> ';
    }

    //下一页
    private function next() {
        if ($this->page == $this->pagenum) {
            return '<span class="disabled">下一页</span>';
        }
        return ' <a href="'.%24this->url.'&page='.(%24this->page+1).'">下一页</a> ';
    }

    //尾页
    private function last() {
        if ($this->pagenum - $this->page > $this->bothnum) {
            return ' ...<a href="'.%24this->url.'&page='.%24this->pagenum.'">'.$this->pagenum.'</a> ';
        }
    }

    //分页信息
    public function showpage() {
        $_page .= $this->first();
        $_page .= $this->pageList();
        $_page .= $this->last();
        $_page .= $this->prev();
        $_page .= $this->next();
        return $_page;
    }
}
?>

</code>

调用方式

<code><?php $_page = new Page($_total,$_pagesize); //其中 $_total 是数据集的总条数,$_pagesize 是每页显示的数量.
?>
</code>

为什么需要操作数据库,楼主要明白分页的逻辑。分页可以分为两部分,第一部分是数据,第二部分是页码条。我们在编写程序的时候其实只需要客户端给我们传一个参数即可,那就是当前页码。
上面的代码其实帮你实现了第二部分,你先通过自己查数据库得到总的条数和自定义一个每页显示数就可以利用此类得到页码条。然后通过客户端传来的当前查询页码写sql得到数据,也即是第一部分。

有可能是通过setUrl()这个返回路径在别的地方进行了封装吧,题主可以看看这篇:PHP实现分页:文本分页和数字分页

里面不是写得很明白吗
构造传参
然后调用showpage

Statement:
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