刚写了mysql数据库操作简单类,顺便也写一下php的分页通用类(pageclass.inc.php),如有不当之处请指教,以下在php4下通过.
<?php
class page{
var $total; //记录总数
var $page; //第几页,如果记录数为空时$page=0这是必然的
var $pagesize; //每页显示记录数,缺省为10
var $url; //分页导航链接
//构造函数
function page($total,$page,$pagesize=10,$url="") {
$this->total = $total;
$this->page = $page;
$this->pagesize = $pagesize;
$this->url = $url;
}
//取得url中的查询字串
function get_page_nav() {
//判断是否已经指定了url,如果没有,则赋值
if (empty($this->url))
{$this->url=$_SERVER['REQUEST_URI'];}
$parse_url = parse_url($this->url);
$url_query = $parse_url["query"]; //得到查询部分
//不带query路径的url
$currpath = $_SERVER["PHP_SELF"];
//替换掉其中的page=$this->page或&page=$this->page部分
$url_query = eregi_replace("(page|&page)=$this->page","",$url_query);
//页码计算
$pagenum = (int) ceil($this->total / $this->pagesize); //总页数,也是最后一页数
$firstpage = 1; //第一页为1
$currpage = $this->page;
$lastpage = $pagenum ; //最后一页依总页数而定,如果总页数为0,则最后一页也为零,否则最后一面为总页数值
$prevpage = ($currpage - 1 > 0 ? $currpage - 1 : 1); //如果当前页减小于或等于0时,那取$page值,$page可能取值为0
$nextpage = ($currpage + 1 > $lastpage ? $currpage : $currpage + 1); //如果当前页加1大于总页数,则取当前页数值
//生成导航字串
$nav = "<b> $currpage </b> of <b> $pagenum </b> pages "; //总页数中的第几页
$nav .= "Show <b> $this->pagesize </b> records a page "; //每一页显示记录数
$nav .= "Total: <b> $this->total </b> records <br>"; //总共记录数
if (substr($url_query,0,1)=="&") {
$nav .= "<a href='$currpath" . "?page=$firstpage" . $url_query . "'>First</a> |"; //首页
$nav .= "<a href='$currpath" . "?page=$nextpage" . $url_query . "'>Next</a> |"; //下一页
$nav .= "<a href='$currpath" . "?page=$prevpage" . $url_query . "'>Prev</a> |"; //前一页
$nav .= "<a href='$currpath" . "?page=$lastpage" . $url_query . "'>Last</a>"; //尾页
} else {
$nav .= "<a href='$currpath" . "?" . $url_query . "&page=$firstpage" . "'>First</a> |"; //首页
$nav .= "<a href='$currpath" . "?" . $url_query . "&page=$nextpage" . "'>Next</a> |"; //下一页
$nav .= "<a href='$currpath" . "?" . $url_query . "&page=$prevpage" . "'>Prev</a> |"; //前一页
$nav .= "<a href='$currpath" . "?" . $url_query . "&page=$lastpage" . "'>Last</a>"; //尾页
}
/////
return $nav;
}
}
?>
test.php如下:
<body>
<table border="1" width="90%">
<?php
require_once("mysqlclass.inc.php"); //调用了前一篇文单写的mysql操作简单类
require_once("pageclass.inc.php");
$mysql = new mysql("front","localhost","root","123");
$mysql->connect();
$mysql->query("select * from ft_products");
$total = $mysql->num_rows();
if (empty($_GET["page"]))
{$currpage = 1;} //如果Querystring中没有指定第几页,则默认为第1页
else {$currpage = intval($_GET["page"]);}
$pagesize = 10; //每页显示记录数
$pageclass = new page($total,$currpage,$pagesize);
$nav = $pageclass->get_page_nav();
$recordstart = ($currpage - 1) * $pagesize; //得到偏移位置
$sql = "select lineid,itemno,catalog,name,imagename,descible from ft_products where lineid is not null order by lineid desc limit $recordstart,$pagesize";
$mysql->query($sql);
?>
<tr> <td colspan="5" align="center"><?php echo($nav);?></td></tr>
<tr bgcolor="#CCCCCC">
<td>Lineid</td>
<td>Itemno</td>
<td>Name</td>
<td>catalog</td>
<td>descible</td>
</tr>
<?php
while ($row = $mysql->fetch_assoc()) {
?>
<tr>
<td><?php echo($row["lineid"]);?></td>
<td><?php echo($row["itemno"]);?></td>
<td><?php echo($row["name"]);?></td>
<td><?php echo($row["catalog"]);?></td>
<td><?php echo($row["descible"]);?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>