我觉得分页用类有点累哈,呵呵我的做法是自己编分页函数,只要用include包含这个公用函数,调一个函数就可以完成分页,好轻松~

解决方案 »

  1.   

    把PageControl.inc,config.php的代码贴来看一看才知道。
      

  2.   

    config.php
      $db_server = "localhost";      //主机
      $db_username = "pcoral";     //用户名
      $db_password = "pcoral";       //密码
      $database_name = "jy";      //数据库名称
      $connect = @mysql_connect ("$db_server","$db_username","$db_password") or die ("无法链接数据库,请与管理员联系");
      $select = mysql_select_db($database_name,$connect) or die("不能连接这个数据库!");
      $table_name1="dj"PageControl.inc.php
    <?
    //分页控制类
    //宇凡 8月15日
    ?>
    <?
    class page //页面属性类,只需要提供offset,当前页,页面总数,就可以进行翻页
    {
    var $sum;//记录总数
    var $now_page;//当前页
    var $offset;//偏移量,即每页显示的记录数
    var $pre_page;//前一页
    var $next_page;//后一页
    var $begin_record;//开始记录号
    var $page_cnt;//总共多少页
    var $page_botton;//在最后一页显示最后剩余的记录。
    var $id;}//记录的IDfunction page($offset,$topage,$sum)
    {
    global $sum,$topage,$offset,$id;
    $this->sum=$sum;
    $this->now_page=$topage;
    $this->next_page=$this->now_page+1;
    $this->pre_page=$this->now_page-1;
    $this->offset=$offset;
    $this->begin_record=$this->now_page*$this->offset-$this->offset;
    $this->page_cnt=ceil($this->sum/$this->offset);if(($this->begin_record+$this->offset)>$this->sum)
    {
    $this->page_botton=$this->sum;
    }
    else
    {
    $this->page_botton=$this->now_page*$this->offset;
    }
    }function showpage()
    {
    global $id;
    print "共".$this->page_cnt."页&nbsp;";
    print "第:".$this->now_page."页<br>";if ($this->now_page!=1)
    {
    @print("<a href=\"$PHP_SELF?id=$id&topage=$this->pre_page\">上一页 </a>&nbsp;\n");
    }if ($this->page_cnt!=0 && $this->now_page!=$this->page_cnt)
    {
    @print("<a href=\"$PHP_SELF?id=$id&topage=$this->next_page\">下一页 </a>&nbsp;\n");
    }for ($y=1;$y<=$this->page_cnt;$y++)
    {
    @print "<a href=\"$PHP_SELF?id=$id&topage=$y\">$y</a>&nbsp;&nbsp;";
    }
    print "<br>";
    }
    ?>
      

  3.   

    我帖个使用我这个类的例子出来现有商品:<br><br>
    <table width="60%" border="0">
      <tr> 
        <td><font color="f24f00">商品名称</font></td>
        <td><font color="f24f00">价  格</font></td>
        <td><font color="f24f00">简  介</font></td>
      </tr>  <?
    $aGoods = new goods_fabled;
    $arr_IDList = array();
    $arr_IDList = $aGoods->GetAllGoods(); $pCtrl = new PageControl($arr_IDList,$p); while ($pCtrl->next_record())
    {
    $ID = $pCtrl->get();
    $aGoods->GetGoodsInfo($ID);
    ?>
      <tr> 
        <td><a href="goods_info.php?ID=<? echo $ID ?>"><? echo $aGoods->Name ?></a></td>
        <td><? echo $aGoods->Price ?> (<? echo $aGoods->Unit ?>)</td>
        <td><? echo $aGoods->Intro ?></td>
      </tr>
    <? }?>
    </table>
      

  4.   

    由这个例子你可以看到我的分页构造器中的int[]参数是ID数组,而这样做的基础是你的所有描述类都有GetInfoByID()这样的方法
      

  5.   

    <?
    class goods_fabled
    {
    var $ID;
    var $Name;
    var $Price;
    var $Unit;
    var $Image;
    var $Intro;
    var $Direct;
    var $TableName; function goods_fabled()
    {
    $this->TableName = "goods_fabled";
    } function GetGoodsInfo($ID)
    {
    $link = new DBOperate;
    $sql = "select * from ".$this->TableName." where ID = ".$ID;
    $link->query($sql);
    $Goods = $link->next_record();
    $this->ID = $ID;
    $this->Name = $Goods["Name"];
    $this->Price = $Goods["Price"];
    $this->Unit = $Goods["Unit"];
    $this->Image = $Goods["Image"];
    $this->Intro = $Goods["Intro"];
    $this->Direct = $Goods["Direct"];
    } function Add()
    {
    $link = new DBOperate;
    $sql = "insert into ".$this->TableName." (ID,Name,Price,Unit,Image,Intro,Direct) values (";
    $sql .="'','".$this->Name."',".$this->Price.",'".$this->Unit."','".$this->Image."','".$this->Intro."','".$this->Direct."')";
    $link->query($sql);
    } function Update()
    {
    $link = new DBOperate;
    $sql = "update ".$this->TableName." set Name = '".$this->Name."',";
    $sql .="set Price = '".$this->Price."',set Unit = '".$this->Unit."',set Image = '".$this->Image."',";
    $sql .="set Intro = '".$this->Intro."',set Direct = '".$this->Direct."' ";
    $sql .="where ID = ".$this->ID;
    $link->query($sql);
    } function Delete($ID)
    {
    $link = new DBOperate;
    $sql = "delete from ".$this->TableName." ";
    $sql .="where ID = ".$this->ID;
    $link->query($sql);
    } function GetAllGoods()
    {
    $result = array();
    $link = new DBOperate;
    $sql = "select ID from ".$this->TableName;
    $link->query($sql);
    for ($i=0;$i<$link->num_rows();$i++)
    {
    $Goods = $link->next_record();
    $result[] = $Goods["ID"];
    }
    return $result;
    } function CheckCommend($ID)
    {
    $result = false;
    $link = new DBOperate;
    $sql = "select * from shop_commend where GoodsType = 'fabled' and GoodsID = ".$ID;
    $link->query($sql);
    if ($link->num_rows() <> 0)
    {
    $result = true;
    }
    return $result;
    } function SearchName($Name)
    {
    $result = array();
    $link = new DBOperate;
    $sql = "select ID from ".$this->TableName." where Name like '%".$Name."%'";
    $link->query($sql);
    for ($i=0;$i<$link->num_rows();$i++)
    {
    $Goods = $link->next_record();
    $result[] = $Goods["ID"];
    }
    return $result;
    }
    }
    ?>
      

  6.   

    宇凡,你的哪个类好像只有每页显示十条记录时是正确的,如果是改成其它的就不可以了。
    我拿你以前的int[si]=si*si+si的例子试了试,每页显示两条,六条,时,结果不正确。
      

  7.   

    哦~~shit~~~我知道是什么地方的错~~一时疏忽啊~~~function next_record()
    {
        $result = false;
        if (($this->UsingIndex != $this->RecordAmount - 1) && ($this->UsingIndex - $this->StartIndex < 9)) {
          $this->UsingIndex += 1;
          $result = true;
        } else {
          $result = false;
        }
        return $result;
    }这个方法中的那个 9 应该改成 $RecordRow_Per_aPage - 1