66. 查询分页数据

来源:互联网 发布:庆元论坛淘宝乐园 编辑:程序博客网 时间:2024/06/03 11:36
上一节中,我们了解到通过循环可以获取一个查询的所有数据,在实际应用中,我们并不希望一次性获取数据表中的所有数据,那样性能会非常的低,因此会使用翻页功能,每页仅显示10条或者20条数据。通过mysql的limit可以很容易的实现分页,limit m,n表示从m行后取n行数据,在PHP中我们需要构造m与n来实现获取某一页的所有数据。假定当前页为$page,每页显示$n条数据,那么m为当前页前面所有的数据,既$m = ($page-1) * $n,在知道了翻页原理以后,那么我们很容易通过构造SQL语句在PHP中实现数据翻页。$page = 2;$n = 2;$m = ($page - 1) * $n;$sql = "select * from user limit $m, $n";$result = mysql_query($sql);//循环获取当前页的数据$data = array();while ($row = mysql_fetch_assoc($result)) {    $data[] = $row;}在上面的例子中,我们使用了$m$n变量来表示偏移量与每页数据条数,但我们推荐使用更有意义的变量名来表示,比如$pagesize, $start, $offset等,这样更容易理解,有助于团队协作开发。

1.翻页改进:
这里写图片描述

empList.php<html><head>    <meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><body><?phprequire_once 'EmpService.class.php';//显示所有用户的信息(表格)//查询数据库$mysqli = new MySQLi('localhost','root','123456','test');if($mysqli->connect_error){    die('connect error' . $mysqli->connect_error);}$mysqli->set_charset('utf8') or die($mysqli->error);$pageSize = 6;$rowCount = 0;$pageNow = 1;if(!empty($_GET['pageNow'])){    $pageNow = $_GET['pageNow'];}//创建 EmpService 对象实例$empService = new EmpService();//调用 getPageCount 方法,获取到共有多少页$pageCount = $empService->getPageCount($pageSize);//调用getEmpListBYPage 获取到应当显示的雇员列表$res2 = $empService->getEmpListByPage($pageNow,$pageSize);/*$sql = "select * from emp limit " . ($pageNow - 1)*$pageSize . ",$pageSize";*///var_dump($sql);die();//$sql = "select * from emp limit 0,2;";//$res2 = $mysqli->query($sql);if(!$res2){    die($mysqli->error);}while($row = $res2->fetch_row()){    $rows[] = $row;}$res2->free();$mysqli->close();?><h1>雇员信息列表</h1><table border="1" cellpadding="0" cellspacing="0" width="80%">    <tr>        <th>id</th>        <th>name</th>        <th>grade</th>        <th>email</th>        <th>salary</th>        <th>操作</th>    </tr>    <?php foreach($rows as $row):?>        <tr>            <td><?php echo $row[0];?></td>            <td><?php echo $row[1];?></td>            <td><?php echo $row[2];?></td>            <td><?php echo $row[3];?></td>            <td><?php echo $row[4];?></td>            <td><a href="">删除用户</a>|<a href="">修改用户</a></td>        </tr>    <?php endforeach;?></table><?phpif($pageNow>1){    $prePage = $pageNow-1;    echo "<a href='empList.php?pageNow=$prePage'>上一页</a>&nbsp;&nbsp;";}echo "当前页{$pageNow}/共{$pageCount}&nbsp;&nbsp;";if($pageNow<$pageCount){    $nextPage = $pageNow+1;    echo "<a href='empList.php?pageNow=$nextPage'>下一页</a>&nbsp;&nbsp;";}//可以使用 for 打印超链接$start = floor($pageNow/10)*10+1;$end = (floor($pageNow/10)+1)*10;//var_dump($start);var_dump($end);var_dump($pageNow);die();for($start;$start<=$end;$start++){    echo "<a href='empList.php?pageNow=$start'>[$start]</a>";}//整体向前翻10页$backup = $pageNow-10;echo "&nbsp;&nbsp;&nbsp;<a href='empList.php?pageNow=$backup'><<<</a>";//整体向后翻10页$fordword = $pageNow+10;echo "&nbsp;&nbsp;&nbsp;<a href='empList.php?pageNow=$fordword'>>>></a>";echo "<br/><br/>";?><form action="">    跳转到:<input type="text" name="pageNow"/>    <input type="submit" value="Go">    <form>        <?php        /*    //打印出页码的超链接            for($i=1;$i<=$pageCount;$i++){                echo "<a href='empList.php?pageNow=$i'>$i</a>&nbsp;&nbsp;";            }        */?></body></html>

封装:

SqlHelper.class.php<?phpclass SqlHelper{    public $conn;    public $dbname = 'test';    public $username = 'root';    public $password = '123456';    public $host = 'localhost';    public function __construct()    {        $this->conn = new MySQLi($this->host,$this->username,$this->password,$this->dbname);        if($this->conn->connect_error){            die('connect error:' . $this->conn->connect_error);        }    }    //执行 dql 语句    public function execute_dql($sql)    {        $res = $this->conn->query($sql) or die($this->conn->error);        return $res;    }    public function execute_dql2($sql)    {        $arr = array();        $res = $this->conn->query($sql) or die($this->conn->error);        //把$res = >$arr        while($row = $res->fetch_row()){            $arr[] = $row;        }        //这里就可以立即关闭资源        $res->free();        return $arr;    }    //考虑分页情况的查询    // $sql1 = "select * from where 表名 limit 0,6"    // $sql2 = "select count(id) from 表名";    public function execute_sql_fenye($sql1,$sql2,&$fenyePage)    {        //这里我们查询了要分页显示的数据        $res = $this->conn->query($sql1) or die($this->conn->error);        //$res=>array();        $arr = array();        while($row = $res->fetch_row()){            $arr[] = $row;        }        $res->free();        $res2 = $this->conn->query($sql2);        if($row = $res2->fetch_row()){            $fenyePage->pageCount = ceil($row[0]/$fenyePage->pageSize);            $fenyePage->rowCount = $row[0];        }        $res2->free();        //把导航信息也分装到 fenyePage 信息中        $navigate = '';        if($fenyePage->pageNow>1){            $prePage = $fenyePage->pageNow-1;            $navigate = "<a href='empList.php?pageNow=$prePage'>上一页</a>&nbsp;&nbsp;";        }        $navigate .= "当前页{$fenyePage->pageNow}/共{$fenyePage->pageCount}&nbsp;&nbsp;";        if($fenyePage->pageNow<$fenyePage->pageCount){            $nextPage = $fenyePage->pageNow+1;            $navigate .= "<a href='empList.php?pageNow=$nextPage'>下一页</a>&nbsp;&nbsp;";        }        $fenyePage->navigate = $navigate;        $fenyePage->res_array = $arr;    }    //执行 dml 语句    public function execute_dml($sql)    {        $b = $this->conn->query($sql);        if(!$b){            return 0;        }else{            if($this->conn->affected_rows>0){                return 1;            }else{                return 2; //没有行受影响            }        }    }    //关闭连接的方法    public function close_connect()    {        if(!empty($this->conn)){        }    }}
EmpService.class.php<?php//一个函数可以获取共有多少页require_once 'SqlHelper.class.php';class EmpService{    function getPageCount($pageSize)    {        //需要查询 $rowCount        $sql = "select count(id) from emp;";        $sqlHelper = new SqlHelper();        $res = $sqlHelper->execute_dql($sql);        //这样就可以计算 $pageCount        if($row = $res->fetch_row()){            $pageCount = ceil($row[0]/$pageSize);        }        //释放资源        $res->free();        //关闭连接        $sqlHelper->close_connect();        return $pageCount;    }    //一个函数可以获取应当显示的雇员信息    public function getEmpListByPage($pageNow,$pageSize)    {        $sql = "select * from emp limit " . ($pageNow - 1)*$pageSize . ",$pageSize";        $sqlHelper = new SqlHelper();        $res = $sqlHelper->execute_dql($sql);        //关闭连接        $sqlHelper->close_connect();        return $res;    }    //第二种使用封装的方式完成的分页(业务逻辑到这里)    function getFenyePage(&$fenyePage)    {        //创建一个 SqlHelper 对象实例        $sqlHelper = new SqlHelper();        $sql1 = "select * from emp limit " . ($fenyePage->pageNow-1)*$fenyePage->pageSize . ",$fenyePage->pageSize;";        $sql2 = "select count(id) from emp;";        $sqlHelper->execute_sql_fenye($sql1,$sql2,$fenyePage);        $sqlHelper->close_connect();    }}
FenyePage.php<?phpclass FenyePage{    public $pageSize = 6;    public $res_array;//显示数据    public $rouCount ;//这是从数据库中获取    public $pageNow;//用户指定    public $pageCount;//这个是计算得到的    public $navigate;  //分页导航}
empList.php<html><head>    <meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><body><h1>雇员信息列表</h1><?phprequire_once 'EmpService.class.php';require_once 'FenyePage.php';//创建一个 FenyePage 对象实例$fenyePage = new FenyePage();//给 fenyePage指定必须的数据$fenyePage->pageNow = 1;$fenyePage->pageSize = 6;if(!empty($_GET['pageNow'])){    $fenyePage->pageNow = $_GET['pageNow'];}$empService = new EmpService();//该方法可以把fenyePage完成$empService->getFenyePage($fenyePage);?><table border="1" cellpadding="0" cellspacing="0" width="80%">    <tr>        <th>id</th>        <th>name</th>        <th>grade</th>        <th>email</th>        <th>salary</th>        <th>操作</th>    </tr>    <?php foreach($fenyePage->res_array as $row):?>        <tr>            <td><?php echo $row[0];?></td>            <td><?php echo $row[1];?></td>            <td><?php echo $row[2];?></td>            <td><?php echo $row[3];?></td>            <td><?php echo $row[4];?></td>            <td><a href="">删除用户</a>|<a href="">修改用户</a></td>        </tr>    <?php endforeach;?></table><?phpecho $fenyePage->navigate;//输出上一页,下一页?></body></html>
0 0
原创粉丝点击