喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: [问题] php分页中的获取参数的菜鸟问题  
 
tiandi (tiandi)
新手上路
Rank: 1
初级会员



UID 62539
精华 0
积分 13
帖子 11
金钱 13 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2004-12-16
状态 离线
[广告]: q m
php分页中的获取参数的菜鸟问题

小弟在试一个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的值并没有传到。不知道是什么原因,请高手赐教!小弟不甚感激!
2008-4-7 04:06 PM#1
查看资料  发短消息  顶部
 
水清无鱼
注册会员
Rank: 2
初级会员



UID 30292
精华 0
积分 182
帖子 489
金钱 182 喜悦币
威望 0
人脉 0
阅读权限 20
注册 2004-7-23
来自 北京
状态 离线
[推荐阅读] 【替别人发】出租广渠门东北角二居室
$_GET['begin_record']



为地震中的遇难者、受伤者、救援者、捐献者祝福……
2008-4-7 04:37 PM#2
查看资料  访问主页  发短消息  QQ  顶部
 
tiandi (tiandi)
新手上路
Rank: 1
初级会员



UID 62539
精华 0
积分 13
帖子 11
金钱 13 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2004-12-16
状态 离线
[推荐阅读] PHP截取字符串的疑问。
谢过“水清无鱼 “了。只是小弟不明白,我并没有在navigate()方法中指定传递方式啊!我尝试用$_POST[]来接收,没有成功,是默认的就是get方式接收吗?小弟没有基础,上手就做,让大虾见笑了!
2008-4-9 12:00 AM#3
查看资料  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.023390 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP