- 论坛徽章:
- 0
|
刚写了mysql数据库操作简单类,顺便也写一下php的分页通用类(pageclass.inc.php),如有不当之处请指教,以下在php4下通过.
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 = " $currpage of $pagenum pages "; //总页数中的第几页
$nav .= "Show $this->pagesize records a page "; //每一页显示记录数
$nav .= "Total: $this->total records
"; //总共记录数
if (substr($url_query,0,1)=="&") {
$nav .= "First |"; //首页
$nav .= "Next |"; //下一页
$nav .= "Prev |"; //前一页
$nav .= "Last"; //尾页
} else {
$nav .= "First |"; //首页
$nav .= "Next |"; //下一页
$nav .= "Prev |"; //前一页
$nav .= "Last"; //尾页
}
/////
return $nav;
}
}
?>
test.php如下:
无标题文档
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);
?>
Lineid
Itemno
Name
catalog
descible
fetch_assoc()) {
?>
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23633/showart_202306.html |
|