<?php
class datalist{
private $key; //主键,用于索引记录条数据
private $tbl; //表名
private $fileds; //查询字段 禁止使用 *号
private $conditions; //SQL查询附加条件
private $orderby; //排序方式
private $pagesize; //每页显示的记录条数
private $curpage; //当前页,需要提前初始化
private $tpl; //列表模板
private $addstr; //附加参数
private $debug; //是否启用调试
private $recordernum; //根据以上条件返回记录数
private $starts; //分页起始数字
private $rs;
private $pages; //单独获取分页
private $db;
private $thissql;
public function __construct($key,$tbl,$fileds,$conditions,$orderby,$pagesize,$curpage,$tpl,$addstr="",$debug=0){
//初始化数据库连接
$this->db=new db();
$this->key=$key;
$this->tbl=$tbl;
$this->fileds=$fileds;
if($conditions==""){
$this->conditions="1=1";
}else{
$this->conditions=$conditions;
}
$this->orderby=$orderby;
$this->pagesize=$pagesize;
if(intval($curpage)==0){
$this->curpage=1;
}else{
$this->curpage=intval($curpage);
}
$this->tpl=$tpl;
if($addstr==""){
$this->addstr=getscriptname() . "?act=list";
}else{
$this->addstr=getscriptname() . "?act=list" . $addstr;
}
$this->debug=$debug;
}
    function __destruct()
    {
        unset($rs);
    }
function getpages(){
return $this->pages;
}
function getthissql(){
return $this->thissql;
}
function showdata(){
echo $this->getdata();
}
function getrs(){
echo "<hr>getrs开始操作<hr>";
echo $this->rs."<hr>";
while($rows=mysql_fetch_array($this->rs)){
//为毛这里不行呢
echo $rows['id'];
}
echo "<hr>getrs操作完毕<hr>";
}
public function getdata(){
//提前调试SQL
if($this->debug==1){
echo $thissql;     
    }
    //计算记录条数
$this->thissql="select count(" . $this->key . ") from " . $this->tbl . " where " . $this->conditions;
$this->recordernum = $this->db->getrsnumbycount($this->thissql);
//计算起始行数
$this->starts=$this->pagesize * ($this->curpage-1);
//组合SQL开始循环数据
$this->thissql="select " . $this->fileds . " from " . $this->tbl . " where " . $this->conditions . " order by " . $this->orderby ." limit " . $this->starts . "," . $this->pagesize;
$result=$this->db->q($this->thissql);
//返回数据集
$this->rs=$result;
echo "<hr>getdata 开始操作!<hr>";
while($rows=mysql_fetch_array($this->rs)){
//这里可以打印出数据
echo $rows['id'];
}
echo "<hr>getdata 操作完毕!<hr>";
//提前调试SQL
    if($this->debug==1){
echo $thissql;     
    }
$tmp_fields = explode(",", $this->fileds);
$tmp_content="";
        while ($row = mysql_fetch_array($result)) {
            $thistpl = $this->tpl;
            foreach ($tmp_fields as $value) {
                $thistpl = str_replace("[$value]", $row[$value], $thistpl);
            }
            $tmp_content.= $thistpl;
        }
$subpage = new subpages($this->pagesize, $this->recordernum, $this->curpage, 5, $this->addstr . "&p=");
$this->pages=$subpage->getsubpages();
return $tmp_content.$subpage->getsubpages();
}
}
?>
$key="id";
$tbl="my_config";
$fileds="id";
$conditions="1=1";
$orderby="id asc";
$pagesize=10;
$curpage=intval(@$_GET['p']);
$tpl="<li><a href='[id]'>[title]----[content]-----[id]</a></li>";
$addstr="";
$id=strget("id");
if($id!==""){
$addstr.="&id=$id";
$conditions.=" and id=" . intval($id);
}
$data=new datalist($key, $tbl, $fileds, $conditions, $orderby, $pagesize, $curpage, $tpl,$addstr);
$data->getdata();
$data->getrs();

解决方案 »

  1.   

    看不懂你想证明什么.
    你在 getdate()方法里 while($rows=mysql_fetch_array($this->rs)) 已经提取出所有查询数据了
    那么在接下来的getrs()方法里就不能再提取出任何数据了。打个拙劣的比方,while($rows=mysql_fetch_array($this->rs))会让你一直向前走,同一条路,第二个while()就只能让你原地踏步,因为你已经到尽头了
      

  2.   

        function getrs(){
            echo "<hr>getrs开始操作<hr>";
            echo $this->rs."<hr>";
            mysql_data_seek($this->rs, 0);//加上这句
            while($rows=mysql_fetch_array($this->rs)){
                //为毛这里不行呢
                echo $rows['id'];
            }
            echo "<hr>getrs操作完毕<hr>";
        }