zoukankan      html  css  js  c++  java
  • 分页(php代码)简

    <?php 
    
        /**
        *
        *    分页类
        */
        class Page
        {
            public $num;    //每页几条
            public $total;    //总条数
            public $p;        //当前页数
    
            //构造方法
            public function __construct($total, $num)
            {
                $this->total = $total;
                $this->num = $num;
                //默认为第一页
                $this->p = isset($_GET['p']) ? $_GET['p'] : 1;
    
            }
    
            /**
            *    获取limit  1 0,5 2 5,5  3 10,5  4 15,5
            */
            public function getLimit()
            {
                $start = ($this->p-1)*$this->num;
    
                $end = $this->num;
    
                $limit = $start.','.$end;
    
                return $limit;
            }
    
            /**
            *    显示页码  上一页  1234567  下一页
            */
            public function show()
            {
                 echo '<pre>';
                // var_dump($_SERVER['HTTP_REFERER']);  
    
                $url = $_SERVER['SCRIPT_NAME'];
    
                //总页数     
                $totalPage = ceil($this->total/$this->num);
    
    
                if ($this->p <= 1) {
    
                    $prev = 1;
                } else {
    
                    $prev = $this->p-1;
                }
                //上一页
                $str = '';
                $str .= "<a href='$url?p=".$prev."'>上一页</a>";
    
    
                //1234567891011
                for ($i=1; $i <= $totalPage; $i++) { 
                    
                    if($i == $this->p) {
    
                        $str .= "<a href='$url?p={$i}' style='color:red'>第{$i}页</a>";
                    } else {
    
                        $str .= "<a href='$url?p={$i}'>第{$i}页</a>";
    
                    }
                }
    
                //下一页
                if($this->p >= $totalPage) {
    
                    $next = $totalPage;
                } else {
    
                    $next = $this->p+1;
                }
    
                $str .= "<a href='$url?p=".$next."'>下一页</a>";
    
                return $str;
            }
    
    
        }
    
        //实例化对象
        //$page = new Page(38,5);
    
        // $page->getLimit();
    
        //echo $page->show();
  • 相关阅读:
    偶的机机升级了
    质疑 Sina.com 的金牌榜[图文]
    一道JAVA作业题
    北京出差总结
    我拿什么奉献给你
    CSDN无限极树PHP+MySQL版
    极大强连通分量的Tarjan算法
    NOI2001 炮兵阵地详解
    单调队列及其应用
    some english website
  • 原文地址:https://www.cnblogs.com/xujing6/p/6270802.html
Copyright © 2011-2022 走看看