php新手之mysql分页PageQuery类


 1   
 2include("dbClass.inc");   
 3class PageQuery extends dbClass {   
 4var $Offset; // 记录偏移量   
 5var $Total; // 记录总数   
 6  
 7var $maxLine; // 记录每页显示记录数   
 8var $result; // 读出的结果   
 9  
10var $TPages; // 总页数   
11var $CPages; // 当前页数   
12  
13var $PageQuery; // 分页显示要传递的参数   
14var $Query; // query 语句   
15var $QueryPart; // " FROM " 以后的 query 部分   
16var $QueryString; // ? 以后部分   
17  
18var $FilePath;   
19  
20// 每页显示行数   
21function PageQuery($pageLine=10) {   
22$this->dbClass();   
23$this->maxLine = $pageLine;   
24}   
25  
26// 记录总数   
27function getTotal(){   
28return $this->Total;   
29}   
30  
31// 显示总页数   
32function getTotalPages() {   
33return $this->TPages;   
34}   
35  
36//显示当前所在页数   
37function getCurrenPages() {   
38return $this->CPages;   
39}   
40  
41function myQuery($sql, $flag=1){   
42GLOBAL $offset;   
43$this->Query = $sql;   
44  
45// 获取文件名   
46//$this->FilePath = $GLOBALS["REQUEST_URI"];   
47$this->FilePath = $GLOBALS["SCRIPT_NAME"];   
48  
49// 获取查询条件   
50$this->QueryString = $GLOBALS["QUERY_STRING"];   
51//echo $this->QueryString . "

<br/>

 1";   
 2  
 3// 截取 " from " 以后的 query 语句   
 4$this->QueryPart = trim(strstr($sql, " from "));   
 5  
 6// 计算偏移量   
 7if (!isset($offset)) $this->Offset = 0;   
 8else $this->Offset = (int)$offset;   
 9  
10  
11  
12  
13// 计算总的记录条数   
14$SQL = "SELECT Count(*) AS total " . $this->QueryPart;   
15$this->result = $this->executeQuery($SQL);   
16$this->Total = mysql_result($this->result,0);   
17  
18// 设置当前页数和总页数   
19$this->TPages = (double)Ceil((double)$this->Total/$this->maxLine);   
20$this->CPages = (double)Floor((double)$this->Offset/$this->maxLine+1);   
21  
22  
23// 根据条件判断,取出所需记录   
24if ($this->Total > 0) {   
25//flag等于1表示要分页,否则不分页   
26if($flag==1)   
27$SQL = $this->Query . " LIMIT " . $this->Offset . " , " . $this->maxLine;   
28else   
29$SQL = $this->Query;   
30echo $SQL . "

<br/>

 1";   
 2$this->$result = $this->executeQuery($SQL);   
 3}   
 4return $this->result;   
 5}   
 6  
 7//**********显示翻页提示栏*************   
 8// 显示首页、下页、上页、尾页   
 9function PageLegend() {   
10$str = "";   
11$i = 0;   
12$first = 0;   
13$next = 0;   
14$prev = 0;   
15$last = 0;   
16  
17$next = $this->Offset + $this->maxLine;   
18$prev = $this->Offset - $this->maxLine;   
19$last = ($this->TPages - 1) * $this->maxLine;   
20  
21GLOBAL $offset;   
22if (!isset($offset)) $this->QueryString .= "&offset=";   
23else{   
24$this->QueryString = substr($this->QueryString,0,strrpos($this->QueryString,'&')) . "&offset=";   
25}   
26  
27if($this->Offset >= $this->maxLine)   
28$str .= "

<a $this-="" .="" ?"="" href=" . $this-&gt;FilePath . ">QueryString . $first . "&gt;首页</a>

1";   
2else $str .= " 首页 ";   
3  
4if($prev >= 0)   
5$str .= "

<a $this-="" .="" ?"="" href=" . $this-&gt;FilePath . ">QueryString . $prev . "&gt;前页</a>

1";   
2else $str .= " 前页 ";   
3  
4if($next < $this->Total)   
5$str .= "

<a $this-="" .="" ?"="" href=" . $this-&gt;FilePath . ">QueryString . $next . "&gt;后页</a>

1";   
2else $str .= " 后页 ";   
3  
4if($this->TPages != 0 && $this->CPages < $this->TPages)   
5$str .= "

<a $this-="" .="" ?"="" href=" . $this-&gt;FilePath . ">QueryString . $last . "&gt;尾页</a>

1";   
2else $str .= " 尾页 ";   
3  
4$str .= " 页次:" . $this->getCurrenPages() . "/" . $this->getTotalPages() . "页 ";   
5$str .= $this->maxLine . "条/页 " . "共" . $this->Total . "条";   
6return $str;   
7}   
8}   
Published At
Categories with 数据库类
Tagged with
comments powered by Disqus