PHP分页显示制作详细讲解

2019-04-10 14:41:08王冬梅

}
else{
  $page_string .= '<a href=?page='.($page+1).'>下一页</a>|<a href=?page='.$page_count.'>尾页</a>';
}
// 获取数据,以二维数组格式返回结果
if( $amount ){
  $sql = "select * from table order by id desc limit ". ($page-1)*$page_size .", $page_size";
  $result = mysql_query($sql);
 
  while ( $row = mysql_fetch_row($result) ){
    $rowset[] = $row;
  }
}else{
  $rowset = array();
}
// 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维数组来显示结果
?>
  4、OO风格代码
  以下代码中的数据库连接是使用的pear db类进行处理
<?php
// FileName: Pager.class.php
// 分页类,这个类仅仅用于处理数据结构,不负责处理显示的工作
Class Pager
{
  var $PageSize;       //每页的数量
  var $CurrentPageID;    //当前的页数
  var $NextPageID;      //下一页
  var $PreviousPageID;    //上一页
  var $numPages;       //总页数
  var $numItems;       //总记录数
  var $isFirstPage;     //是否第一页
  var $isLastPage;      //是否最后一页
  var $sql;         //sql查询语句
 
 function Pager($option)
  {
    global $db;
    $this->_setOptions($option);
    // 总条数
    if ( !isset($this->numItems) )
    {
      $res = $db->query($this->sql);
      $this->numItems = $res->numRows();
    }
    // 总页数
    if ( $this->numItems > 0 )
    {
      if ( $this->numItems < $this->PageSize ){ $this->numPages = 1; }
      if ( $this->numItems % $this->PageSize )
      {
        $this->numPages= (int)($this->numItems / $this->PageSize) + 1;
      }
      else
      {
        $this->numPages = $this->numItems / $this->PageSize;
      }
    }
    else
    {
      $this->numPages = 0;
    }
   
    switch ( $this->CurrentPageID )
    {
      case $this->numPages == 1:
        $this->isFirstPage = true;
        $this->isLastPage = true;
        break;
      case 1:
        $this->isFirstPage = true;
        $this->isLastPage = false;
        break;
      case $this->numPages:
        $this->isFirstPage = false;
        $this->isLastPage = true;
        break;
相关文章 大家在看