新手系列之四:刚学习了多菜鸟BOLG上的一种分页方法,但不知如何正确使用
原文分页代码:
<?php
/**********************
* 通用分页类
* 作者: 多菜鸟
* 邮箱: kingerq AT msn DOT com
* 时间: 2006-04-17
* 来源:http://blog.csdn.net/kingerq
* 实例:
$p = new Page( 60, 10 );
$p->trunStr = array( "First", "Prev", "Next", "End" );//设置分页显示字符
echo $p->printPage();//输出分页字符串,也可以trunUp(),trunNum(),trunDown()和jumpTo()分开来输出显示
//echo $p->limitStr();//显示MYSQL中LIMIT部分字符串
//echo $p->pageCount();//总页数
**********************
*/
class Page {
var $perPage = 5; //每页显示的页码数
var $paramStr = ""; //分页参数字符串变量,连接地址
var $currentPage = 1; //当前页码
var $paramName = "pageNum"; //翻页参数名称
var $limitStr = ""; //MYSQL中LIMIT参数字符串
var $pageSize = 20; //每页记录数
var $trunStr = array( "首页", "上页", "下页", "末页" ); //翻页字符
/*
* 构造函数
* $recordCount 为记录总数量
* $pageSize 为每页显示记录数
*/
function Page( $recordCount = 0, $pageSize = 0 ) {
if( ! is_numeric( $recordCount ) || ! is_numeric( $pageSize ) ) return;
//记录总数
if( intval( $recordCount ) == 0 )
$recordCount = $this->recordCount;
else
$this->recordCount = $recordCount;
//每页显示记录数
if( intval( $pageSize ) == 0 )
$pageSize = $this->pageSize;
else
$this->pageSize = $pageSize;
$this->currentPage();//当前页码处理
$this->paramStr();//当前页地址参数
//为当前页定界
if( $this->currentPage > $this->pageCount() )
$this->currentPage = $this->pageCount() < 1 ? 1 : $this->pageCount();
}
function printPage() {
$pageStr = $this->trunUp(); //向上
$pageStr .= $this->trunNum(); //数字页码
$pageStr .= $this->trunDown(); //向下
$pageStr .= $this->jumpTo(); //跳转
return $pageStr;
}
//向上翻处理
function trunUp() {
$pageStr = "";
if( $this->currentPage == 1 ) {
$pageStr .= $this->trunStr[0] . " " . $this->trunStr[1] . " ";
}else {
$pageStr .= "<a href=\"".$this->paramStr."1\" title=\"".$this->trunStr[0]."\">".$this->trunStr[0]."</a> ";
$pageStr .= "<a href=\"".$this->paramStr.($this->currentPage - 1)."\" title=\"".$this->trunStr[1]."\">".$this->trunStr[1]."</a> ";
}
return $pageStr;
}
//每页显示的页码连接
function trunNum() {
$pageStr = "";
$num = 1;
while( $this->currentPage - $this->perPage * $num > 0 ) $num++;
$start = ( $num - 1 ) * $this->perPage;
$end = $this->pageCount() < $this->perPage * $num ? $this->pageCount() : $this->perPage * $num;
for( $i = $start + 1; $i <= $end; $i++ ) {
$pageStr .= $i != $this->currentPage ? "<a href=\"".$this->paramStr."$i\">$i</a> " : "<strong>$i</strong> ";
}
return $pageStr;
}
//向下翻处理
function trunDown() {
$pageStr = "";
if( $this->pageCount() == $this->currentPage || $this->pageCount() == 0 ) {
$pageStr .= $this->trunStr[2] . " " . $this->trunStr[3] . " ";
} else {
$pageStr .= "<a href=\"".$this->paramStr.($this->currentPage + 1)."\" title=\"".$this->trunStr[2]."\">".$this->trunStr[2]."</a> ";
$pageStr .= "<a href=\"".$this->paramStr.$this->pageCount()."\" title=\"".$this->trunStr[3]."\">".$this->trunStr[3]."</a> ";
}
return $pageStr;
}
//跳转
function jumpTo() {
$pageStr = "<select onchange=\"location.href=this.options[this.selectedIndex].value\">\n";
for($i = 1; $i <= $this->pageCount(); $i++){
if($this->currentPage == $i) $selected = " selected";
else $selected = "";
$pageStr .= "<option value='".$this->paramStr.$i."'$selected>$i</option>\n";
}
$pageStr .= "</select>";
return $pageStr;
}
//当前页参数
function paramStr() {
unset( $_GET[$this->paramName] );//去掉翻页参数
if( count( $_GET ) ) {
foreach( $_GET AS $param => $value ) {
$this->paramStr .= $this->paramStr ? "&" : "?";
$this->paramStr .= $param . "=" . $value;
}
$this->paramStr .= "&".$this->paramName."=";
} else {
$this->paramStr = "?$this->paramName=";
}
}
//取得当前页码
function currentPage() {
if( isset( $_GET[$this->paramName] ) && intval( $_GET[$this->paramName] ) > 0 ) {
$this->currentPage = intval( $_GET[$this->paramName] );
}
return $this->currentPage;
}
//总页数
function pageCount() {
return ceil( $this->recordCount / $this->pageSize );//总页数
}
//MYSQL中LIMIT部分字符串
function limitStr() {
return " LIMIT " . ( ( $this->currentPage - 1 ) * $this->pageSize ) . ", " . $this->pageSize;
}
//设置每页显示页码数,页码数至少大于1
function setPerPage($num = 5) {
if( intval($num) > 1 )
$this->perPage = intval($num);
}
}
?>
以上我保存到pageft.php中,然后调用:
<?
include "pageft.php";
$mysql_link=mysql_connect($host,$user,$password);
mysql_select_db($bdb,$mysql_link);
$type=$_GET["cat"];
$sql="select * from t_goods where goodstype='$type'";
$str = "select count(*) from t_goods where goodstype='$type'";
$db->query($str); //这里报错了。。请大家解释一下
$db->next_record();
$recordcount = $db->f(0);//记录总数
$p =New Page($recordcount, 10 );
$p->trunStr = array( "First", "Prev", "Next", "End" );//设置分页显示字符
$result=mysql_query($sql);
while($row=mysql_fetch_array($result))
{
?>
//省略显示内容
<?
}
?>
<?php
echo $p->printPage();
?>
我屏蔽了
$str = "select count(*) from t_goods where goodstype='$type'";
$db->query($str); //这里报错了。。请大家解释一下
$db->next_record();
$recordcount = $db->f(0);//记录总数
后,将$recordcount用常数50代替,但显示结果中,每页显示记录数却是显示的都是50,不知哪里出了问题???