设为首页 收藏本站
查看: 684|回复: 0

[经验分享] php实现google样式的分页

[复制链接]

尚未签到

发表于 2017-4-2 12:43:36 | 显示全部楼层 |阅读模式
  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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359140-1-1.html 上篇帖子: php 加密解密类(含公钥) 下篇帖子: php GregorianToJD函数-历法函数库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表