php实现google样式的分页

本文详细介绍了如何使用PHP类库实现分页算法,并将其与MySQL数据库结合,有效处理海量数据分页问题。通过实例演示了分页类的使用方法,包括设置参数、获取分页信息等步骤,最终展示了分页样式和执行效果。同时,文章还提到可以通过继承分页类来定制不同的分页样式,增强应用的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Pager.class.php 代码如下

class Pager {
    /**
     *int 总页数
     * */
    protected $pageTotal;
    /**
     *int 上一页
     * */
    protected $previous;
    /**
     *int 下一页
     * */
    protected $next;
    /**
     *int 中间页起始序号
     * */
    protected $startPage;
    /**
     *int 中间页终止序号
     * */
    protected $endPage;
    /**
     *int 记录总数
     * */
    protected $recorbTotal;
    /**
     *int 每页显示记录数
     * */
    protected $pageSize;
    /**
     *int 当前显示页
     * */
    protected $currentPage;
    /**
     *string 基url地址
     * */
    protected $baseUri;
    
    /**
     * @return string 获取基url地址
     */
    public function getBaseUri() {
        return $this->baseUri;
    }
    
    /**
     * @return int 获取当前显示页
     */
    public function getCurrentPage() {
        return $this->currentPage;
    }
    
    /**
     * @return int 获取每页显示记录数
     */
    public function getPageSize() {
        return $this->pageSize;
    }
    
    /**
     * @return int 获取记录总数
     */
    public function getRecorbTotal() {
        return $this->recorbTotal;
    }
    
    /**
     * @param string $baseUri 设置基url地址
     */
    public function setBaseUri($baseUri) {
        $this->baseUri = $baseUri;
    }
    
    /**
     * @param int $currentPage 设置当前显示页
     */
    public function setCurrentPage($currentPage) {
        $this->currentPage=$currentPage;
    }
    
    /**
     * @param int $pageSize 设置每页显示记录数
     */
    public function setPageSize($pageSize) {
        $this->pageSize = $pageSize;
    }
    
    /**
     * @param int $recorbTotal 设置获取记录总数
     */
    public function setRecorbTotal($recorbTotal) {
        $this->recorbTotal = $recorbTotal;
    }
    
    /**
     *构造函数
     * */
    public function __construct()
    {
        $this->pageTotal = 0;
        $this->previous = 0;
        $this->next = 0;
        $this->startPage = 0;
        $this->endPage = 0;
        
        $this->pageSize = 20;
        $this->currentPage = 0;
    }
    
    /**
     *分页算法
     * */
    private function arithmetic() {
        if ($this->currentPage < 1)
            $this->currentPage = 1;
        
        $this->pageTotal = floor ( $this->recorbTotal / $this->pageSize ) + ($this->recorbTotal % $this->pageSize > 0 ? 1 : 0);
        
        if ($this->currentPage > 1 && $this->currentPage > $this->pageTotal)
            header ( 'location:' . $this->baseUri . 'page=' . $this->pageTotal );
        
        $this->next = $this->currentPage + 1;
        $this->previous = $this->currentPage - 1;
        
        $this->startPage = ($this->currentPage + 5) > $this->pageTotal ? $this->pageTotal - 10 : $this->currentPage - 5;
        $this->endPage = $this->currentPage < 5 ? 11 : $this->currentPage + 5;
        
        if ($this->startPage < 1)
            $this->startPage = 1;
        
        if ($this->pageTotal < $this->endPage)
            $this->endPage = $this->pageTotal;
    }
    
    /**
     *分页样式
     * */
    protected function pageStyle() {
        $result = "共" . $this->pageTotal . "页      ";
        
        if ($this->currentPage > 1)
            $result .= "<a href=\"" . $this->baseUri . "page=1\"><font style=\"font-family:webdings\">9</font></a>  <a href=\"" . $this->baseUri . "page=$this->previous\"><font style=\"font-family:webdings\">3</font></a>";
        else
            $result .= "<font style=\"font-family:webdings\">9</font> <font style=\"font-family:webdings\">3</font>";
        
        for($i = $this->startPage; $i <= $this->endPage; $i ++) {
            if ($this->currentPage == $i)
                $result .= "  <font color=\"#ff0000\">$i</font>";
            else
                $result .= "  <a href=\"" . $this->baseUri . "page=$i\">$i</a>";
        }
        
        if ($this->currentPage != $this->pageTotal) {
            $result .= "  <a href=\"" . $this->baseUri . "page=$this->next\"><font style=\"font-family:webdings\">4</font></a>";
            $result .= "  <a href=\"" . $this->baseUri . "page=$this->pageTotal\"><font style=\"font-family:webdings\">:</font></a>";
        } else {
            $result .= " <font style=\"font-family:webdings\">4</font> <font style=\"font-family:webdings\">:</font>";
        }
        return $result;
    }
    
    /**
     *执行分页
     * */
    public function execute() {
        if ($this->baseUri != "" && $this->recorbTotal == 0)
            return "";
        $this->arithmetic();
        return $this->pageStyle ();
    }
}
调用代码(test.php 代码如下)

include_once 'Pager.class.php';
$pager=new Pager();
if (isset ( $_GET ['page'] ) && ! emptyempty ( $_GET ['page'] ))
    $pager->setCurrentPage($_GET ['page']);
else
    $pager->setCurrentPage(1);
    
$pager->setRecorbTotal(1000);
$pager->setBaseUri("test.php?");
echo $pager->execute();
 

数据库结合 mysql 通用存储过程分页 海量数据分页  就是一个完美的分页了

 

我们还可继承 Pager 类重写pageStyle方法就可以有不同的样式了. yes ok

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值