免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1885 | 回复: 0
打印 上一主题 下一主题

php新手之mysql分页PageQuery类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-27 09:48 |只看该作者 |倒序浏览
  提供完全可以使用的类,使用时请初始化各函数!
dbClass();
        $this->maxLine = $pageLine;
      }
      
      // 记录总数
    function getTotal(){
        return $this->Total;
    }
      
      // 显示总页数
    function getTotalPages() {
        return $this->TPages;
    }
    //显示当前所在页数
    function getCurrenPages() {         
        return $this->CPages;
    }
   
    function myQuery($sql, $flag=1){
            GLOBAL $offset;
            $this->Query = $sql;
        
        // 获取文件名
        //$this->FilePath = $GLOBALS["REQUEST_URI"];
            $this->FilePath = $GLOBALS["SCRIPT_NAME"];
            
            // 获取查询条件
            $this->QueryString = $GLOBALS["QUERY_STRING"];            
            //echo $this->QueryString . "
";            
            
            // 截取 " from " 以后的 query 语句
            $this->QueryPart = trim(strstr($sql, " from "));
            
            // 计算偏移量
            if (!isset($offset)) $this->Offset = 0;
            else $this->Offset = (int)$offset;
            
            
        
        
        // 计算总的记录条数
        $SQL = "SELECT Count(*) AS total " . $this->QueryPart;
        $this->result = $this->executeQuery($SQL);
            $this->Total = mysql_result($this->result,0);
            
            // 设置当前页数和总页数
        $this->TPages = (double)Ceil((double)$this->Total/$this->maxLine);
        $this->CPages = (double)Floor((double)$this->Offset/$this->maxLine+1);
        
        
        // 根据条件判断,取出所需记录
        if ($this->Total > 0) {
            //flag等于1表示要分页,否则不分页
            if($flag==1)
                $SQL = $this->Query . " LIMIT " . $this->Offset . " , " . $this->maxLine;
            else
                $SQL = $this->Query;            
            echo $SQL . "
";
            $this->$result = $this->executeQuery($SQL);
        }
        return $this->result;
    }
   
    //**********显示翻页提示栏*************  
    // 显示首页、下页、上页、尾页
    function PageLegend() {        
        $str = "";
        $i = 0;
        $first = 0;
        $next = 0;
        $prev = 0;
        $last = 0;
   
            $next = $this->Offset + $this->maxLine;
            $prev = $this->Offset - $this->maxLine;
            $last = ($this->TPages - 1) * $this->maxLine;
            
            GLOBAL $offset;
            if (!isset($offset)) $this->QueryString .= "&offset=";
            else{
                $this->QueryString = substr($this->QueryString,0,strrpos($this->QueryString,'&')) . "&offset=";
            }
            
            if($this->Offset >= $this->maxLine)
            $str .=  " FilePath . "?" . $this->QueryString . $first . ">首页 ";
            else $str .= " 首页 ";
        
        if($prev >= 0)
            $str .=  " FilePath . "?" . $this->QueryString . $prev . ">前页 ";
        else $str .= " 前页 ";
        
        if($next Total)
            $str .=  " FilePath . "?" . $this->QueryString . $next . ">后页 ";
        else $str .= " 后页 ";
        
        if($this->TPages != 0 && $this->CPages TPages)
            $str .=  " FilePath . "?" . $this->QueryString . $last . ">尾页";
        else $str .= " 尾页 ";
        $str .= " 页次:" . $this->getCurrenPages() . "/" . $this->getTotalPages() . "页 ";
        $str .= $this->maxLine . "条/页 " . "共" . $this->Total . "条";
            return $str;
    }
}
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/3052/showart_8770.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP