分页技术

1.获取当前页码数据

 

页码        Sql语句
1           select * from my_user limit 0 ,10
2           select * from my_user limit 10 ,10
3           select * from my_user limit 20 ,10

 

结论:
    $pageNo:页码
    $startNo:起始位置
    $pageSize:页面大小
公式:起始位置 $startNo=($pageNo-1)*$pageSize;

 

2.页码获取

用户点击页面的页码,传递当前的页码

 

3.如何获取总页码

记录数          页码        计算
60              6           60/10
80              8            80/10
82              9            82/10   有小数就进位

 

结论:
    $rowCount:总记录数
    $pageCount:总页数
公式:$pageCount=ceil($rowCount/10);
 
 
步骤及代码展示
 

fenye.php

<?php 

    //自动加载类
    spl_autoload_register(function ($class_name){
        require "./{$class_name}.class.php";
    });
    $link=MySqlDB::createLink();
   
    $pageSize=10;
    //第一步:获取总记录数
    $rowCount=$link->selectRowCount("select * from my_user");

    //第二步:求出总页数
    $pageCount=ceil($rowCount/10);

    //第四步:获取当前页面并求起始位置
    $pageNo=$_GET['pageNo'] ?? 1;
    $startNo=ceil($pageNo-1)*$pageSize;   

    //第五步:获取当前页面数据,并遍历显示
    $result=$link->fetchAll("select * from my_user limit $startNo,$pageSize");

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style> 
        td{
            border: 1px solid black;
            text-align:center;
            width: 150px;
            height: 50px;
        }
    
    </style>
    <table>
        <tr>
            <th>编号</th>
            <th>用户</th>
            <th>密码</th>
            <th>描述</th>
            <!-- 第五步 显示数据 -->
            <?php foreach($result as $v):?>
                <tr>
                    <td><?=$v['u_id']?></td>
                    <td><?=$v['u_name']?></td>
                    <td><?=$v['u_pwd']?></td>
                    <td><?=$v['u_desc']?></td>
                </tr>
            <?php endforeach?>
        </tr>
    </table>
        <!-- 第三步:循环显示页码 -->
    <?php for($i=1;$i<=$pageCount;$i++):?>
            <a href="?pageNo=<?=$i?>"><?=$i?></a>
    <?php endfor?>
</body>
</html>

 

MySqlDB 类

<?php
    //数据库连接单例模式
    /**
     * 以后开的时候 注意事项
     * 尽量在一个函数中 实现一种功能
     * 之后,可以在构造函数中去调用函数
     */
    class MySqlDB{
        //创建私有属性
        private static $instence;
        private $host;
        private $user;
        private $pwd;
        private $port;
        private $db;
        private $charset;
        private $link;
        //实例化
        private function __construct($data){
            $this->initArg($data);
            $this->databaseLinke();
        }
        private function __clone(){
        }
        //创建对象
        public static function createLink($data=array()){
            if(!self::$instence instanceof self){
                return self::$instence =new self($data);
            }
            return  self::$instence;
        }

        //初始化参数
        private function initArg($data){
            $this->host=$data['host'] ?? "127.0.0.1";
            $this->user=$data['user'] ?? "root";
            $this->pwd=$data['pwd'] ?? "root";
            $this->port=$data['port'] ?? "3306";
            $this->db=$data['db'] ?? "my_db";
        }

        private function databaseLinke(){
            $this->link=mysqli_connect($this->host,$this->user,$this->pwd,$this->db,$this->port);
            if(mysqli_connect_error()){
                echo "数据库连接失败<br>";
                echo "错误代码". mysqli_connect_errno();
                exit;
            }
            mysqli_set_charset($this->link,$this->charset);

        }

        /**
         * 执行增、删、改、查
         * 
         */
        private function exec($sql){
            $rs=mysqli_query($this->link,$sql);
            if(mysqli_errno($this->link)){
                echo "此操作执行出错<br>";
                echo "错误代码". mysqli_error($this->link)."<br>";
                echo "错误的sql语句". $sql."<br>";
                exit;
            }
            return $rs;
        }

        /**
         * 作用:获取表中的记录了行数,并返回
         * @param string sql语句
         * @return int 返回记录的行数
         */
        public function selectRowCount($sql){
            $rs= $this->exec($sql);
            return mysqli_num_rows($rs);
        }

        /**
         * 做用:查询操作,返回结果对象
        */
        private function fetch($sql){
           return $rs=$this->exec($sql);
           
        }

        /**
         * 二次封装fetchAll函数,返回二维数组;
         * @return array 结果集合
         */
        public function fetchAll($sql,$type='assoc'){
            $type=$this->getType($type);
            $rs=$this->fetch($sql);
            return mysqli_fetch_all($rs,$type);
        }

        /**
         * 返回结果集的类型
         * @return string 结果的类型
         */
        private function getType($type){
            switch($type){
                case 'num':
                    return MYSQLI_NUM;
                case 'both':
                    return MYSQLI_BOTH;
                default:
                    return MYSQLI_ASSOC;
            }
        }


    }

 

 
 
 
 
 
 
posted @ 2021-01-20 14:37  WhiteSpace  阅读(72)  评论(0编辑  收藏  举报