• 基于搜索内容自己写一个分页功能


      前言:由于部分查询数据库代码是经过修饰的,所以看起来有点不对劲,但是不影响思路 

      1.先写一个生成分页链接的类

    <?php
    /**
     * Created by PhpStorm.
     * User: lxj
     * Date: 2017/7/25
     * Time: 14:42
     */
    class Page{
        protected $total;//总的结果数
        protected $page_size;//每页显示的记录数
        protected $page_now;//当前是第几页
        protected $url;//跳转链接时的URL
        public function __construct($total, $page_size, $page_now, $url){
            $this->total = $total;
            $this->page_size = $page_size;
            $this->page_now = $page_now;
            $this->url = $url;
        }
        public function createPage(){
            $url = $this->url."page=";
            //首页
            $html = <<<PAGEHTML
            <ul class="pageBar">
                <li>
                    <a href="$this->url">首页</a>
                </li>
    PAGEHTML;
            //分页列表,循环生成
            $page_count = ceil($this->total / $this->page_size);
            for($i = $this->page_now - 5;$i < $this->page_now + 5; $i++){
                if($i <= 1 || $i >= $page_count){
                    continue;
                }
                $html .= <<<PAGEHTML
                <li>
                    <a href="$url$i">$i</a>
                </li>
    PAGEHTML;
            }
            //尾页
            $html .= <<<PAGEHTML
            <li>
                <a href="$url$page_count">尾页</a>
            </li>
    PAGEHTML;
            $html .= " 当前为第".$this->page_now."页,共".$page_count."页,数据共".$this->total."条";
            //返回分页链接
            return $html;
        }
    }

      2.美化分页条的样式

      

    .pageBar{
        margin:15px 0 0 25px;
    }
    .pageBar li{
        display: inline-block;
        width:35px;
        text-align: center;
        border:1px solid silver;
        background: whitesmoke;
    }
    .pageBar a:link{
        color:steelblue;
        display: inline-block;
        width:35px;
        height:20px;
        text-decoration: none;
    }
    .pageBar a:hover{
        color:red;
        display: inline-block;
        width:35px;
        height:20px;
        text-decoration: none;
    }
    .pageBar a:visited{
        color:burlywood;
        display: inline-block;
        width:35px;
        height:20px;
        text-decoration: none;
    }

      3.调用分页类并传递参数

    //生成分页链接start
            if($_GET['companyName']){
                $query_para['companyName'] = $_GET['companyName'];
            }
            if($_GET['addr_province']){
                $query_para['addr_province'] = $_GET['addr_province'];
            }
            if($_GET['area']){
                $query_para['area'] = $_GET['area'];
            }
            if($_GET['beginDT']){
                $query_para['beginDT'] = $_GET['beginDT'];
            }
            if($_GET['endDT']){
                $query_para['endDT'] = $_GET['endDT'];
            }if(count($query_para)){
                foreach($query_para as $key=>$value){
              //如果是页面参数则不加入传递的链接链接中
    if($key == 'page'){ continue; } $query_string .= $key.'='.$value.'&'; } } $nowUrl = "agtProductConsumeList.php?".$query_string; $pageObj = new Page($this->count,$this->one_page_num,$this->page_now,$nowUrl); $page_bar = $pageObj->createPage(); //生成分页链接end

      4.根据分页传递的页数搜索 

        public $one_page_num = 5;//每页显示记录数
        public $count;//总的记录数
        public $page_now;//当前页数
    
        protected function getInfo(){
         $ws = " where user_id > 10000";
         //搜索结果总数
    $rowCount = $this->mysqlObj->sr("select count(*) as num from user $ws"); $this->count = $rowCount['num']; $first_row = ($this->page_now - 1) * $this->one_page_num; $last_row = $this->one_page_num; $ws .= " LIMIT ".$first_row.",".$last_row;
         $result = $this->mysqlObj->sr("select count(*) as num from user $ws"); }
  • 相关阅读:
    Gitlab的安装汉化及问题解决
    Python-Day15 JavaScript/DOM
    Python-Day12 Python mysql and ORM
    Python-Day11 RabbitMQ/redis
    Python-Day9 Paramiko模块/进程/线程/RabbitMQ队列
    Python-Day7 面向对象进阶/异常处理/Socket
    使用CryptoJS解决微信小程序用户信息解密
    How to: Configure an Azure SQL Database firewall using the Azure Portal
    iOS 开发: 自定义相册, 保存多张图片到自定义相册中
    解决iOS8.0以上系统下无法通过代码创建被删除过的同名相册 ( iOS 8 Photos framework: Create a albums with iOS8 )
  • 原文地址:https://www.cnblogs.com/wxdblog/p/7238617.html
Copyright © 2020-2023  润新知