php入门mysql分页PageQuery类

2016-02-19 14:10 16 1 收藏

下面图老师小编要跟大家分享php入门mysql分页PageQuery类,简单的过程中其实暗藏玄机,还是要细心学习,喜欢还请记得收藏哦!

【 tulaoshi.com - 编程语言 】

。?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 语句
   &nbs

MySQL教程是:php入门mysql分页PageQuery类。p;        $this-QueryPart = trim(strstr($sql, " from "));
           
            // 计算偏移量
            if (!isset($offset)) $this-Offset = 0;
            else $this-Offset = (int)$offset;                            

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)       // 计算总的记录条数
        $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;
           
         &nb


MySQL教程是:php入门mysql分页PageQuery类。sp;  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 .= " 尾页 ";

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

        $str .= " 页次:" . $this-getCurrenPages() . "/" . $this-getTotalPages() . "页 ";
        $str .= $this-maxLine . "条/页 " . "共" . $this-Total . "条";
            return $str;
    }
}

来源:https://www.tulaoshi.com/n/20160219/1606319.html

延伸阅读
标签: Java JAVA基础
在JSP中经常要用到查询数据库中的数据,同常我们的做法是使用SQL语句“select * from tablename order by id desc”,这样的做法有一个缺点,当数据库很大的时候查询的速度会变的很慢,在ASP中有一种方法 "select top "&recpage&" * from tablename where id not in (select top "&(recpage*(currentpage-1))&" ...
标签: Web开发
?php /* 需求,建立一个test数据库,在里边建一个test表,里面就 只要id字段,输入一下数据就可以啦。。 由于水平有限,难免出错。。 */ $conn = mysql_connect("localhost","root",""); $maxnum = 2;  //每页显示记录条数 mysql_select_db("test", $conn); $query1 = "SELECT CO...
标签: Web开发
其它的一些,比如分页类,异常类(用于信息提示),文件操作类(未完成),经常用到的工具类及验证输入的表单验证类(ASP版,配合前台JS版使用更佳): 分页类Pager % Class Pager Private IUrl Private IPage Private IParam Private IPageSize Private IPageCount Private IRecordCount Pri...
最基本的分页方式: SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT ... 在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就是确保使用了索引: 举例来说,如果实际SQL类似下面语句,那么在category_id, id两列上建立复合索引比较好: SELECT * FROM articles WHERE category_id = 123 ORDER BY id LIMIT...
标签: PHP
  我承认我不是PHP的领导者。然而,在看了一些PHP的信息之后,我认为有一些功能需要添加到其中来处理数据库连接和整合XML。要做到这一点,我想我可以创建一个处理连接MySQL和使用PHP中的domxml功能来提供XML输出的类。然后我就可以在PHP脚本的任何地方声明这个类并且在需要使用它的时候可以提供XML功能。   我假设人们使用PHP是原...

经验教程

13

收藏

49
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部