| 网站首页 | JAVA文章 | AppServers | Web开发 | 应用开发 | 资源下载 | 论坛
    学好外语能够获得更多的国外先进编程技术  [enadd  2006年12月25日]        
设为首页 加入收藏 联系站长
您现在的位置: 编程笔记网 >> Web开发 >> 网络编程 >> PHP编程 >> PHP经验技巧 >> 文章正文
PHP应用分页显示制作详细讲解            【字体:
PHP应用分页显示制作详细讲解
作者:未知    文章来源:-    点击数:    更新时间:2006-12-8

  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;
    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方法保证数据库兼容性

上一页  [1] [2] [3] [4] [5] 下一页  

文章录入:enadd    责任编辑:enadd 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • 用php发送带附件的Email

  • 用php实现gb2312和unicode间…

  • 解决RHAS3中Apache2的PHP上传…

  • 令你的网站获得任意Google P…

  • PHP和JAVA的XML-RPC中文问题…

  • 使用php通过Socket进行发信源…

  • PHP下实现端口复用/劫持

  • PHP链接ACCESS数据库最简单的…

  • IIS 不用 rewrite 实现页面静…

  • PHP发现安全漏洞

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 管理登录 |