PHP应用分页显示制作详细讲解

2019-04-07 19:27:03于海丽

     $this->isLastPage = false;
     break;
    case $this->numPages:
     $this->isFirstPage = false;
     $this->isLastPage = true;
     break;
    default:
     $this->isFirstPage = false;
     $this->isLastPage = false;
   }

   if ( $this->numPages > 1 )
   {
    if ( !$this->isLastPage ) { $this->NextPageID = $this->CurrentPageID + 1; }
    if ( !$this->isFirstPage ) { $this->PreviousPageID = $this->CurrentPageID - 1; }
   }

   return true;
  }

  /***
  *
  * 返回结果集的数据库连接
  * 在结果集比较大的时候可以直接使用这个方法获得数据库连接,然后在类之外遍历,这样开销较小
  * 如果结果集不是很大,可以直接使用getPageData的方式获取二维数组格式的结果
  * getPageData方法也是调用本方法来获取结果的
  *
  ***/

  function getDataLink()
  {
   if ( $this->numItems )
   {
    global $db;

    $PageID = $this->CurrentPageID;

    $from = ($PageID - 1)*$this->PageSize;
    $count = $this->PageSize;
    $link = $db->limitQuery($this->sql, $from, $count); //使用Pear DB::limitQuery方法保证数据库兼容性

    return $link;
   }
   else
   {
    return false;
   }
  }

  /***
  *
  * 以二维数组的格式返回结果集
  *
  ***/

  function getPageData()
  {
   if ( $this->numItems )
   {
    if ( $res = $this->getDataLink() )
    {
     if ( $res->numRows() )
     {
      while ( $row = $res->fetchRow() )
      {
       $result[] = $row;
      }
     }
     else
     {
      $result = array();
     }

     return $result;
    }
    else
    {
     return false;
    }
   }
   else
   {
    return false;
   }
  }

  function _setOptions($option)
  {
   $allow_options = array(
     'PageSize',
     'CurrentPageID',
     'sql',
     'numItems'
   );

  foreach ( $option as $key => $value )
  {
   if ( in_array($key, $allow_options) && ($value != null) )
   {
    $this->$key = $value;
   }
  }

  return true;
 }
}
?>
<?php
// FileName: test_pager.php
// 这是一段简单的示例代码,前边省略了使用pear db类建立数据库连接的代码

相关文章 大家在看