<?
//-----------------------
//  通用分页控制……
// 21bird  2002.8.22
//-----------------------
//class pagecontrol
class pagecontrol{
  //properties
  var $m_RecordAmount;
  var $m_PageAmount;
  var $m_currPage = 1;
  var $RecordRow_Per_aPage = 10;
  var $m_nextPage;
  var $m_prevPage;
  var $m_usingIndex;
  var $m_startIndex;
  var $r;
  //methods
  function pagecontrol($r, $RecordAmount, $p)
  {
    $this->r = $r;
    if (($p == "") || (round($p) == 0)){
        $p=1;
    }
    $this->m_currPage = $p;
    $this->m_RecordAmount = $RecordAmount;
    $this->m_PageAmount = Ceil($this->m_RecordAmount/$this->RecordRow_Per_aPage);
    $this->m_startIndex = ($this->m_currPage - 1) * $this->RecordRow_Per_aPage;
    $this->m_usingIndex = $this->m_startIndex;
    //calulate the current page
    if($this->m_currPage != $this->m_PageAmount)
      $this->m_nextPage = $this->m_currPage + 1;
    else
      $this->m_nextPage = $this->m_PageAmount;
    if($this->m_currPage != 1)
      $this->m_prevPage = $this->m_currPage - 1;
    else
      $this->m_prevPage = 1;
  }
  function Next_Record()
  {
    if(($this->m_usingIndex != $this->m_RecordAmount - 1)||
          ($this->m_usingIndex < ($this->RecordRow_Per_aPage)))
    {
      $this->m_usingIndex ++;
      $result = 1;
    }
    else
      $result = 0;
    return $result;
  }
  function link_first()
  {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=1\">首页</a>";
  }
  function link_last()
  {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" . $this->m_PageAmount . "\">尾页</a>";
  }
  function link_prev()
  {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_prevPage . "\">前页</a>";
  }
  function link_next()
  {
    echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?p=" .$this->m_nextPage . "\">后页</a>";
  }
  function link_any()
  {
    echo "<form method=get action=\"" . $_SERVER['PHP_SELF'] . "\">直接到<input type=text name=p size = 3>页<input type=submit value = 'Go!!!'></form>";
  }
  function getCurrent()
  {
    mssql_data_seek($this->r, $this->m_usingIndex);
    list($result) = mssql_fetch_array($this->r);
    return $result;
  }
}
?>凑个热闹^_*

解决方案 »

  1.   

    那个$r是执行sql之后建立的数据库连接id。
    下面是使用这个类的……
    <?
       include("pagecontrol.php"); 
       //中间的自己写吧^_^
       $sql = "select * from main_log";
       $r = mssql_query($sql, $log_db->Link_id);
       $rows = mssql_num_rows($r);
       $aCtrl = new pagecontrol($r, $rows, $HTTP_GET_VARS["p"]);
       echo "<br>总记录数=" . $aCtrl->m_RecordAmount;
       echo "<br>总页数=" . $aCtrl->m_PageAmount;
       echo "<br>当前页=" . $aCtrl->m_currPage;
       echo "<br>每页记录数=" . $aCtrl->RecordRow_Per_aPage;
       echo "<br>下一页=" . $aCtrl->m_nextPage;
       echo "<br>上一页=" . $aCtrl->m_prevPage;
       echo "<br>正在使用索引=" . $aCtrl->m_usingIndex;
       echo "<br>本页开始索引=" . $aCtrl->m_startIndex;
       echo "<br><br>";
       $aCtrl->link_first();
       echo "&nbsp;&nbsp;";
       $aCtrl->link_prev();
       echo "&nbsp;&nbsp;";
       $aCtrl->link_next();
       echo "&nbsp;&nbsp;";
       $aCtrl->link_last();
       echo "&nbsp;&nbsp;";
       $aCtrl->link_any();
       echo "&nbsp;&nbsp;";
       echo "<p>";
       while($aCtrl->Next_Record())
       {
         $UsingIndex=$aCtrl->UsingIndex+1;
         $aRow = $aCtrl->getCurrent();     echo display_digital(21474836471);
         echo $aRow["id"] . "<br>";
         echo $aRow["name"] . "<br>";
         echo "<p>";
       }
    ?>
    to 楼主:你这样好像会执行sql 多次……
      

  2.   

    在MSSQL的mssql_query($string,$int);
    其中$string可以是SQL SERVER存储过程的调用.这样就要自己写一个类似于limit的存储过程,在SQL SERVER他虽有TOP这项功能,但它的翻页,我们那时有个写VB的同事封装了一个用游标翻页的存储过程,由于年事以久,且不是自己封装的所一存储过程的代码找不见了,现在说一下调用过程,望能引发大家的一些思维:应该不是误导^_^
    $string="exec web_ScrollPage 'fo03t001','complex,description,isroomreqd,isexcludegrp,isautoassign','complex,description,isroomreqd,isexcludegrp,isautoassign',".$PAGE_ROWS.",".$AddValue;
    exec web_ScrollPage 'fo03t001',---这一段是掉用存储过程,及所要访问的table
    'complex,description,isroomreqd,isexcludegrp,isautoassign',----要读的字段
    'complex,description,isroomreqd,isexcludegrp,isautoassign',----存储过程中建立临时表所要的字段
    ".$PAGE_ROWS.",".$AddValue-----每页的行数,及开始的位置在存储过程中是先建临时表,移游标到$AddValue所在的位置,然后移动游标一条条将记录读到临时表里,最后再读一次临时,删掉临时表,就这样是不是很烦.
    不过在读大量数据是用数组翻页就不如这样了^_^
      

  3.   

    楼上的,谢谢,这种方法我在asp高级编程中见到过,上面有原原本本的存储过程代码,不过我觉得这种方法没有我的方法好。可能在asp中比较好
      

  4.   

    vivanboy(我是谁?):
    是呀,我在忙着呢,没细看:(