小弟在试一个php分页的类,在测试过程中遇到一个小问题,具体的编码如下:
conf.php文件:
<?
$dbhost = "localhost";
$dbuser = "root";
$dbpasswd = "";
$dbname = "images";
$page_max_line = 4;
?>
page.php文件:
<?
include "conf.php";
//分页显示数据库中的数据
class page{
var $sql; //所要显示数据的SQL查询语句
var $max_line; //每页现实最多行数
var $begin_record; //所要显示记录的首行序号
var $total_records; //记录总数
var $current_records; //本页读取的记录数
var $result; //读出的结果
var $total_pages; //总页数
var $current_pages; //当前页数
var $arr_page_query; //数组,包含分页显示需要传递的参数
//构造函数 __construct()
//输入参数:$pmax_line:每页显示最多的行数
function page($pmax_line){
$this->max_line = $pmax_line;
$this->begin_record = 0;
}
//构造函数 __destruct()
function __destruct(){
}
//get函数
function __get($property_name){
if(isset($this->$property_name)){
return($this->$property_name);
}else{
return(null);
}
}
//set函数
function __set($property_name,$value){
$this->$property_name = $value;
}
//函数名:read_data
//根据SQL查询语句从表中读取相应的记录,返回一个二维数组result[记录号][字段名]
function read_data(){
$psql = $this->sql;
//查询数据,数据库链接等信息应在类调用的外部实现
$result = mysql_query($psql) or die(mysql_error());
$this->total_records = mysql_num_rows($result);
//利用LIMIT关键字获取本页所要显示的记录
if($this->total_records>0){
$psql = $psql." LIMIT ".$this->begin_record.",".$this->max_line;
echo $psql."<br>";
$result = mysql_query($psql) or die(mysql_error());
$this->current_records = mysql_num_rows($result);
//将查询结果放在result数组中
$i=0;
while($row = mysql_fetch_array($result)){
$this->result[$i] = $row;
$i++;
}
}
}
//显示首页,下页,上页,末页
function navigate(){
//获取总页数,当前页信息
$this->total_pages = ceil($this->total_records/$this->max_line);
$this->current_page = $this->begin_record/$this->max_line+1;
echo "<div align='center'>";
echo "<font color=red size=2>第".$this->current_page."页/共".$this->total_pages."页</font>";
echo " ";
//获取将要导航到的分页的初始记录
$first = 0;
$next = $this->begin_record+$this->max_line;
$prev = $this->begin_record-$this->max_line;
$last = ($this->total_pages-1)*$this->max_line;
//生成导航连接
if($this->begin_record >= $this->max_line)
echo "<a href=".$_SERVER['PHP_SELF']."?begin_record=".$first.">首页</a>";
if($prev > 0)
echo "<a href=".$_SERVER['PHP_SELF']."?begin_record=".$prev.">上一页</a>";
if($next < $this->total_records)
echo "<a href=".$_SERVER['PHP_SELF']."?begin_record=".$next.">下一页</a>";
if($this->total_pages != 0&& $this->current_page < $this->total_pages)
echo "<a href=".$_SERVER['PHP_SELF']."?begin_record=".$last.">末页</a>";
echo "</div>";
}
}
?>
test_page1.php文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?
include "page.php";
function display_pic(){
global $view;
include "conf.php";
$conn = mysql_connect($dbhost,$dbuser,$dbpasswd);
mysql_query("SET NAMES 'GBK'");
mysql_select_db($dbname);
$view->__set("sql","select * from pic order by id desc");
$view->read_data();
if($view->current_records == 0){
echo "<Tr><td colbegin_record = 4></td></tr>";
return;
}
for($i=0;$i<$view->current_records;$i++){
$caption = $view->result[$i]['caption'];
$caption = iconv("gb2312","utf-8",$caption);
echo $caption."<br>";
}
mysql_close($conn);
}
$view = new page($page_max_line);
if(isset($begin_record)){
$view->__set("begin_record",$begin_record);
}else{
$view->__set("begin_record",0);
}
display_pic();
$view->navigate();
?>
在标题中输入http://localhost/test_page1.php的时候可以显示一定数目的记录,但是在点击[下一页]的时候,题目倒是可以正确显示跳转的参数http://localhost/test_page1.php?begin_record=4($page_max_line = 4)。但是下面的显示并没有改变,也就是说begin_record的值并没有传到read_data()中,就连在test_page1.php中的if(isset($begin_record))都判断为假,所以这个begin_record的值并没有传到。不知道是什么原因,请高手赐教!小弟不甚感激!