您的位置:首页 > 编程语言 > PHP开发

php 分页操作(三)

2015-08-22 22:44 531 查看
使用分层模式,对信息进行分页

分页的时候,需要知道总页数(pageCount)、当前页数(pageNow可以由编程人员自己定义)、总共有多少条信息(rowCount需要使用sql语句获取)、每页显示的信息数(pageSize可以由编程人员自己定义)

所以,我们需要通过函数获得总页数(pageCount)和总共有多少条信息(rowCount需要使用sql语句获取)

下面我们看一下,数据处理图示



代码块如下:

fenye.php

<?php

    require_once 'fenyeService.class.php';

$pageSize=5;   //每页显示信息量

$rowCount=0;    //信息总量

$pageNow=2;    //当前页数

$pageCount=0;   //总页数

if(!empty($_GET['pageNow'])){

      $pageNow=$_GET['pageNow'];

     }

$fenyeService =new fenyeService();

    //创建fenyeService

    $pageCount=$fenyeService->getPageCount($pageSize);

    //调用getPageCount($pageSize)获得总页数

$res2=$fenyeService->getfenyelistByPage($pageNow,$pageSize);

    //调用getfenyelistByPage($pageNow,$pageSize)获得信息列表

echo "<table border='1' width='700px'>";

    echo "<tr>";

    echo "<td>员工id</td><td>员工姓名</td><td>员工等级</td><td>员工邮箱</td><td>员工薪水</td></tr>";

    //我们需要通过数组取出信息

/*while($row=mysql_fetch_assoc($res2)){

      echo "<tr>";

      echo "<td>" .$row['id']."</td><td>" .$row['name']."</td><td>" .$row['grade']."</td>

     <td>" .$row['emil']."</td><td>" .$row['salary']."</td>";

      echo "</tr>";

     }*/

 for($i=0;$i<count($res2);$i++){

       $row=$res2[$i];

       echo "<tr>";

       echo "<td>" .$row['id']."</td><td>" .$row['name']."</td><td>" .$row['grade']."</td>

     <td>" .$row['emil']."</td><td>" .$row['salary']."</td>";

       echo "</tr>";

      }

     echo "</table>";

 if($pageNow>1){

      $prepage=$pageNow-1;

      echo "<a href='fenye.php?pageNow=$prepage'>上一页  </a>";

     }

      for($i=1;$i<=$pageCount;$i++){

      echo "<a href='fenye.php?pageNow=$i'>$i  </a>";

     }

      if($pageNow<$pageCount){

      $nextpage=$pageNow+1;

      echo "<a href='fenye.php?pageNow=$nextpage'>下一页  </a>";

     }

?>

fenyeService.class.php

<?php

     require_once 'SqlHelper.class.php';

    class fenyeService{

    //获取总页数

   function getPageCount($pageSize){

       $sql="select count(id) from emp";  //根据id获得数据总量

   $sqlHelper=new SqlHelper();   //实例化SqlHelper对象

       $res=$sqlHelper->execute_dql($sql);

       if($row=mysql_fetch_row($res)){  //使用mysql_fetch_row()函数可以获得数据总量

   //这里使用if语句,是因为返回值是一个整型数据,不需要while语句

              $pageCount=ceil($row[0]/$pageSize);

          }

       mysql_free_result($res);   //释放资源

       $sqlHelper->close_conn();   //关闭连接

       return $pageCount;    //返回pageCount数值

   }

     //获得分页信息

    function getfenyelistByPage($pageNow,$pageSize){

  

          $sql="select * from emp limit ".($pageNow-1)*$pageSize.",$pageSize";

          $sqlHelper=new SqlHelper();

          $res=$sqlHelper->execute_dql2($sql);   //得到结果集

          $sqlHelper->close_conn();

  //mysql_free_result($res);   

  //该方法不能在此处释放掉,因为这个函数在其他位置被调用,一旦资源释放,结果集就不会返回

         

          return $res;

      }

}

?>

SqlHelper.class.php

<?php

  class SqlHelper{

      public $conn;

      public $dbname="shop";

      public $username="root";

      public $password="123456";

      public $host="localhost";

      public function __construct(){

          $this->conn=mysql_connect($this->host,$this->username,$this->password);

          mysql_query("set names utf8",$this->conn) ;

          if(!$this->conn){

              die("连接失败".mysql_error());

          }

          mysql_
4000
select_db($this->dbname,$this->conn);

      }

      //执行dql语句

      public function execute_dql($sql){

          $res=mysql_query($sql,$this->conn) or die(mysql_error()); 

          return $res;

      } 

  //把mysql_fetch_assoc()函数得到的结果集转换为数组形式的数据

      public function execute_dql2($sql){

          $arr=array();

          $res=mysql_query($sql,$this->conn) or die(mysql_error());

          $i=0;

          while($row=mysql_fetch_assoc($res)){//一行一行取出数据

              $arr[$i++]=$row;   //取出的数据,赋值给数组

          }

          mysql_free_result($res);  //结果集赋值给数组后,就可以立即释放资源

          return $arr;

      }

      //关闭连接

      public function close_conn(){

          if(!empty($this->conn)){

             mysql_close($this->conn); 

          } 

      }

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: