`

php实现google样式的分页

    博客分类:
  • php
阅读更多

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

 

分享到:
评论

相关推荐

    php+mysql 的数字分页 类似google的分页带有样式有例子

    php+mysql 的数字分页 类似google的分页带有样式有例子

    php仿谷歌分页类.zip

    PHP通用分页类(仿Google样式)。本代码是用于分页用的,只需提供记录总数与每页显示数两个参数,无需指定URL,链接由程序生成。方便用于检索结果分页,表单采用GET方法提交,可保证在诸如查询之,删除之类的操作时,...

    PHP分页类,完美版(可以植入到MVC框架中)

    超强分页类,四种分页模式,默认采用类似baidu,google的分页风格 支持自定义风格,自定义样式,同时支持PHP4和PHP5 里面deom.php有一个示例,下载下来直接可以用,适合新手使用!

    PHP通用分页类page.php[仿google分页]

    (仿Google样式) ** 只需提供记录总数与每页显示数两个参数。(已附详细使用说明..) ** 无需指定URL,链接由程序生成。方便用于检索结果分页。 ** 表单采用GET方法提交,可保证在诸如查询之,删除之类的操作时,不丢失...

    php 高级 分页类 源码

    * description:超强分页类,四种分页模式,默认采用类似baidu,google的分页风格。 * 2.0增加功能:支持自定义风格,自定义样式,同时支持PHP4和PHP5, * to see detail,please visit ...

    PHP 分页类(模仿google)-面试题目解答

    上机题目是要写一个仿google分页的类,当要取类似9/2的最大整数,却怎么也想不起函数ceil的名字,晕了半天。 最后测试程序没错误,但是就是不能正常显示,后来(回家后)一查才知道是语句:for($i=0;$i++;$i&lt;9)...

    歪酷CMS v1.0 Release gbk build 20120914

    歪酷网站管理系统(歪酷CMS)是一款基于THINKPHP框架开发的PHP+MYSQL网站建站程序,本程序实现了文章和栏目的批量动态管理,支持栏目无限分类,引入RBAC权限管理机制(下一版本将会具体实现),实现多管理员管理,程序辅助...

    AddressBook:示例 PHP 地址簿

    地址簿 示例 PHP 地址簿 ... 除了使用 Google OAUTH 和导入 Google 联系人之外,我还想添加一些其他功能/修订: 单独的联系人组 排序和搜索选项 分页 单个联系人视图 自定义字段 个人资料照片 vCard 导入和导出

    java开源包3

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包4

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    阿赛HTML在线编辑器 v10

    功能:样式、字体、字号、颜色、背景颜色、加粗、斜体、下划线、立体字、发光字、上标字、下标字、古书排版、编号、对齐、缩进、链接、Word粘贴、Html清理、参考线、自动排版、查找替换、内容分页、插入代码、插入...

    阿赛HTML在线编辑器 v8.zip

    阿赛HTML在线编辑器是一款简洁,兼容性极强的HTML在线编辑器(经测试已完美兼容IE全系、火狐、谷歌),可以方便地整合到各种网站系统中(经测试已完美支持ASP、PHP、.NET、JSP),摒弃常规的框架编辑器模式,采用...

    js使用小技巧

    style标签里的第一个样式 document.styleSheets[0].rules[0] 防止点击空链接时,页面往往重置到页首端。 ()"&gt;word 上一网页源 asp: request.servervariables("HTTP_REFERER") javascript: document....

    java开源包1

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包11

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包2

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包6

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包5

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包10

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

    java开源包8

    GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以...

Global site tag (gtag.js) - Google Analytics