1.數(shù)據(jù)庫連接的類dbClass.inc。
<?php
/**
* a class use to connect the MySQL database and do some query
*/
class dbClass {
private $hostName = "localhost:3306";
private $dbName = "ebooklib";
private $Login = "root";
private $Password = "";
private $conn;
private $result;
function dbClass(){
$this->conn = mysql_connect("$this->hostName","$this->Login","$this->Password");
mysql_select_db("$this->dbName", $this->conn);
}
function executeQuery($sql){
$this->result = mysql_query("$sql",$this->conn);
return $this->result;
}
function closeConn(){
mysql_close($this->conn);
}
}
?>
2.解決分頁問題的PageQuery.inc
<?php
include("dbClass.inc");
class PageQuery extends dbClass {
private $Offset; // 記錄偏移量
private $Total; // 記錄總數(shù)
private $maxLine; // 記錄每頁顯示記錄數(shù)
private $result; // 讀出的結(jié)果
private $TPages; // 總頁數(shù)
private $CPages; // 當前頁數(shù)
private $PageQuery; // 分頁顯示要傳遞的參數(shù)
private $Query; // query 語句
private $QueryPart; // " FROM " 以后的 query 部分
private $QueryString; // ? 以后部分
private $FilePath;
// 每頁顯示行數(shù)
function PageQuery($pageLine=10) {
$this->dbClass();
$this->maxLine = $pageLine;
}
// 記錄總數(shù)
function getTotal(){
return $this->Total;
}
// 顯示總頁數(shù)
function getTotalPages() {
return $this->TPages;
}
//顯示當前所在頁數(shù)
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;
// 計算總的記錄條數(shù)
$SQL = "SELECT Count(*) AS total " . $this->QueryPart;
$this->result = $this->executeQuery($SQL);
$this->Total = mysql_result($this->result,0);
// 設(shè)置當前頁數(shù)和總頁數(shù)
$this->TPages = (double)Ceil((double)$this->Total/$this->maxLine);
$this->CPages = (double)Floor((double)$this->Offset/$this->maxLine+1);
// 根據(jù)條件判斷,取出所需記錄
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;
}
}
?>
3.用于顯示結(jié)果的mysql_result_all.inc
<?
function mysql_result_all($result,$format="") {
echo "<table $format><tr>";
for($i=0;$i<mysql_num_fields($result);$i++) {
echo "<th>".mysql_field_name($result,$i)."</th>";
}
echo "</tr>";
while($row = mysql_fetch_array($result) ) {
for($i=0;$i<mysql_num_fields($result);$i++) {
echo "<td>".$row[$i]."</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
4.顯示頁面的代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>php&mysql分頁顯示</title>
</head>
<body>
<?php
include("PageQuery.inc");
$pq = new PageQuery(5); // 獲取Connection
$res=$pq->myQuery("select * from users"); // 執(zhí)行查詢
require("mysql_result_all.inc");
mysql_result_all($res,"border=1");
echo $pq->PageLegend(2); // 翻頁欄
?>
</body>
</html>