首 页中国梦想方达科技Loveshine'Blog绿区文章绿区下载绿区图像客户留言绿区论坛繁體中文
设为首页
加入收藏
联系我们
您当前的位置:绿色生活社区 -> 数 据 库 -> Mysql -> 文章内容 退出登录 用户管理
栏目导航
· Access · Mysql
· Mssql · Oracle
热门文章
· [组图] 网页导航条素..
· QQ空间相册密码破解..
· 【推荐收藏】【MSDN..
· WIN2003产品密钥
· 越狱全集高速下载
· 新东方在线 4+1 网络..
· 空间代码----最新去..
· 用于格式化后硬盘数..
· 新东方在线 4+1 网络..
· 新东方在线 4+1 网络..
相关文章
· php与mysql三日通
· 使用GeoIP和PHP构建..
· MySQL分页模型(Pagi..
· 基于PHP+MySQL的聊天..
· 在phpMyAdmin使用用..
· 关于PHP中操作MySQL..
· 我的一个php_mysql分..
· 超级服务器apache+m..
· 超级服务器apache+m..
· Apache/PHP/MySQL f..
php新手之mysql分页PageQuery类
作者:不详  来源:网络  发布时间:2008-1-20 16:32:53  发布人:loveshine

减小字体 增大字体

<?php
include("dbClass.inc");
class PageQuery extends dbClass {
    var $Offset;             // 记录偏移量
    var $Total;             // 记录总数
      
    var $maxLine;             // 记录每页显示记录数
    var $result;             // 读出的结果
    
    var $TPages;             // 总页数
    var $CPages;             // 当前页数
    
    var $PageQuery;         // 分页显示要传递的参数
    var $Query;             // query 语句
    var $QueryPart;         // " FROM " 以后的 query 部分
    var $QueryString;         // ? 以后部分  
      
    var $FilePath;
            
    // 每页显示行数
    function PageQuery($pageLine=10) {    
        $this->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 . "<br>";            
            
            // 截取 " 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 . "<br>";
            $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 .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $first . ">首页</A> ";
            else $str .= " 首页 ";
        
        if($prev >= 0)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $prev . ">前页</A> ";
        else $str .= " 前页 ";
        
        if($next < $this->Total)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $next . ">后页</A> ";
        else $str .= " 后页 ";
        
        if($this->TPages != 0 && $this->CPages < $this->TPages)
            $str .=  " <A href=" . $this->FilePath . "?" . $this->QueryString . $last . ">尾页</A>";
        else $str .= " 尾页 ";

        $str .= " 页次:" . $this->getCurrenPages() . "/" . $this->getTotalPages() . "页 ";
        $str .= $this->maxLine . "条/页 " . "共" . $this->Total . "条";
            return $str;
    }
}
?>
[] [返回上一页] [打 印] [收 藏]
下一篇文章:基于MySQL的BBS设计(3)
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 绿区社区
Copyright © 2002-2006 1Lives.Com. All Rights Reserved .
冀ICP备06009849号