这是分页的类:
<?
/**************************************************************************
 * 程序文件:   class.page.php                       
 * 建立时间:   2002年11月18日
 * 建立人:  
 * 最后修改: 2002年11月22日 10:05:45
 * 修改人:
功能描述:
$datapager->datapager($dbconnection, $query, $pagesize, $querytousetocountrecords);
参数说明:
$dbconnection 数据库连接
$query SQL查询语句(*without any limit x, y on the end)
$pagesize 每页显示的记录数
$querytousetocountrecords   可选项, 当select语句用COUNT(*)取代字段返回数大于1时可使用,用于统计总页数和记录总数执行查询返回相应数值
$datapager->execute($page,$pagesize);
参数说明:
$pagesize 每页显示的记录数
$page 当前页数建立执行另外一个查询
$datapager->loadquery($query, $pagesize, $querytousetocountrecords); get a string containing a link to display the next/previous page
$str = $datapager->nextpage($html, $althtml = "");
$str = $datapager->prevpage($html, $althtml = "");$html 如 <a href='thispage.php?page=%page%'>Next</a>
$althtml 当前页是最后一页时 - 默认为 ""获得包含全部页的连接的字符串
$datapager->pagelinks($linkhtml, $currenthtml = "%page%", $separator = " | ");
where
$linkhtml 如 <a href='thispage.php?page=%page%'>%page%</a>
$currenthtml 当前页数, 如 "%page%"
$separator 每个页数的分割符号返回结果含义:
$datapager->page // 当前页页码
$datapager->pagesize // 每页记录数
$datapager->recordcount // 记录总数
$datapager->pagecount // 总页数
**************************************************************************/
class datapager{ var $mainquery;
var $countquery;
var $results;
var $connection;
var $pagesize;
var $pagecount;
var $page;
var $recordcount;
var $querydone;
var $rowscount; function datapager($conn = 0, $query  = "", $pagesize = 10, $countquery = ""){
$this->connection = $conn;
$this->querydone = false;
$this->pagesize = $pagesize;
$this->loadquery($query, $pagesize, $countquery);
} function loadquery($query, $pagesize=0, $countquery=""){
$this->querydone = false;
if( $pagesize > 0 )$this->pagesize = $pagesize;
$this->results = $this->pagecount = $this->page = $this->recordcount = 0;
if( $query == "" || strtoupper(substr($query, 0, 6)) != "SELECT") return false;
$this->mainquery = $query;
if( $countquery == "" ){
$frompos = strpos( strtoupper($query), "FROM");
$this->countquery = "SELECT COUNT(*) ".substr($query, $frompos);
}else{
$this->countquery = $countquery;
}
if( $this->connection ){
$res = mysql_query($this->countquery, $this->connection)or die(mysql_error());
if( $res && mysql_num_rows($res) != 1 ){
$res = mysql_query($this->mainquery, $this->connection)or die(mysql_error());
$this->recordcount = mysql_num_rows($res);
}else{
list($this->recordcount) = mysql_fetch_row($res);
}
if($this->pagesize!=0)
$this->pagecount = ceil($this->recordcount / $this->pagesize);
else
                $this->pagecount = 0;
$this->page = 1;
$this->querydone = true;
mysql_free_result($res);
return true;
}
return false;
}
    //后页
function nextpage($html, $althtml = ""){
if( $this->page < $this->pagecount ){
return str_replace("%page%", $this->page+1, $html);
}else{
return $althtml;
}
}
    //前页
function prevpage($html, $althtml = ""){
if( $this->page > 1 ){
return str_replace("%page%", $this->page-1, $html);
}else{
return $althtml;
}
}
//首页
function firstpage($html, $althtml = ""){
if( $this->page > 1 ){
return str_replace("%page%", "1", $html);
}else{
return $althtml;
}
}
//尾页
function lastpage($html, $althtml = ""){
if( $this->page > 0 && $this->page < $this->pagecount){
return str_replace("%page%", $this->pagecount, $html);
}else{
return $althtml;
}
}
    //前十页page_int>0
function prevpage_10($html, $althtml = "",$page_int=""){
if( $page_int > 0 ){
return str_replace("%page%", ($page_int-1)*10, $html);
}else{
return $althtml;
}
}
//后十页page_int<(pagecount-1)\10;$this->pagecount-1
function nextpage_10($html, $althtml = "",$page_int=""){
if( $page_int < floor(($this->pagecount-1)/10)){
return str_replace("%page%", 10*($page_int+1)+1, $html);
}else{
return $althtml;
}
}
function pagelinks($linkhtml, $currenthtml = "%page%", $separator = " | "){
$str = "";
for( $i = 1; $i <= $this->pagecount; $i++){
if( $i != $this->page ){
$str .= str_replace("%page%", $i, $linkhtml);
}else{
$str .=  str_replace("%page%", $i, $currenthtml);
}
if( $i < $this->pagecount ) $str .= $separator;
}
return $str; 
} function execute( $page = 1, $pagesize = 10){
if( $this->querydone == false ) return 0;
if( $page < 1 ) $page = 1;
if( $pagesize > $this->recordcount ) $pagesize = $this->recordcount;
$this->pagesize = $pagesize;
$this->page = $page;
if($this->pagesize==0)
    $this->pagecount = 0;
else
    $this->pagecount = ceil($this->recordcount / $this->pagesize);
if( $this->page > $this->pagecount ) $this->page = $this->pagecount; // do query $sql = $this->mainquery." LIMIT ".(($this->page-1) * $this->pagesize).",".$this->pagesize;
$this->results = mysql_query($sql);
$this->rowscount = mysql_num_rows($this->results);
return $this->results;
}}

解决方案 »

  1.   

    使用:
    <?
    /********分页******/
    $dp = new datapager();
    /******判断当前页码是否设置,没有赋值成1*****/
    if( !isset( $page ) ){
    $page = 1;
    }
    /********设置每页显示的记录数************/
    $page_int=Floor($page/10);
    $pagesize = 9;
    $conn=$mysql_server->mysql_link;/***数据库连接信息***/
    $dp->datapager($conn, $sql,$pagesize);
    $res = $dp->execute($page,$pagesize);
    ?><?
    if($dp->rowscount!=0){
      $firstpage_img="<img src=\"./images/dream_first.gif\" width=17 height=18 alt=' Backward 10 pages ' align=absmiddle border=0>";
      $prepage_img="<img src=\"./images/dream_pre.gif\" width=17 height=18 alt=' Prev page ' align=absmiddle border=0>";
      $nextpage_img="<img src=\"./images/dream_next.gif\" width=17 height=18 alt=' Next Page ' align=absmiddle border=0>";
      $lastpage_img="<img src=\"./images/dream_end.gif\" width=17 height=18 alt=' Forward 10 pages ' align=absmiddle border=0>";
              echo $dp->prevpage_10("<a href='products.php?page=%page%&id=$id'>".$firstpage_img."</a>",$firstpage_img,$page_int);
      ?>
                                  <?echo $dp->prevpage("<a href='products.php?page=%page%&id=$id'>".$prepage_img."</a>",$prepage_img);?>
                                  <?                        
    for($i=(1+10*$page_int);$i<=(10*($page_int+1));$i++)
    {
    if($i>$dp->pagecount) continue;
    if($page==$i)
    echo "<font color=#000000><b><font color=\"#FF3300\">$i</font></b></font><font color=#FFFFFF>.</font>";
    else
    echo"<a href='products.php?page=$i&id=$id'>[$i]</a><font color=#FFFFFF>.</font>";
    }
    ?>
                                  <?echo $dp->nextpage("<a href='products.php?page=%page%&id=$id'>".$nextpage_img."</a>",$nextpage_img);?>
                                  <?echo $dp->nextpage_10("<a href='products.php?page=%page%&id=$id'>".$lastpage_img."</a>",$lastpage_img,$page_int);
       }?>
      

  2.   

    请参考一下这个:
     我们在浏览网页时,经常看到分页显示的页面。如果想把大量数据提供给浏览者,分页显示是个非常实用的方法。在下面的文章中,我们将介绍如何用PHP和MS SQL Server实现对数据库中纪录的分页显示。
        在本例中,我们用mssql_num_rows()函数得到当前查询的记录数,结合页面大小SgPageSize,得到当前记录集要显示的页面数,为分页显示打下了基础。mssql_data_seek()函数是分页显示的关键函数,该函数的第二个参数标示当前纪录的偏移量,根据这个偏移量就可以找到要显示的页面。
      分页显示源程序:
    <br><html>
    <head>
    <title>PHP分页</title>
    </head>
    <body>
    <?
    //为了便于理解和更快地应用到工作中去,我们以MS SQL Server的NorthWind数据库ustomers表为例。
    $gPageSize= 10; //每页显示的记录数
    $hostname = "localhost"; //MSSQL Server
    $dbuser = "sa"; //用户名
    $dbpasswd = "1111111"; //密码
    //连接数据库
    $id = mssql_connect($hostname,$dbuser,$dbpasswd) or die("无法连接数据库服务器!");
    //选择数据库,为了方便,这里以MSSQL Server的NorthWind数据库为例
    $db = mssql_select_db("northwind",$id) or die("无法连接数据库!");
    //以Customers表为例,构建查询字符串
    $query = "select * from Customers";
    //执行查询语句
    $rresult = mssql_query($query) or die("无法执行SQL:$query");
    //$page变量标示当前显示的页
    if(!isset($page)) $page=1;
    if($page==0) $page=1;
    //得到当前查询到的纪录数 $nNumRows
    if(($nNumRows= mssql_num_rows($rresult))<=0)
    {
    echo "<p align=center>没有纪录";
    exit;
    };
    //得到最大页码数MaxPage
    $MaxPage = (int)ceil($nNumRows/$gPageSize);
    if((int)$page > $MaxPage)
    $page=$maxPage;
    ?>
    <table align="center" width="80%" border=0> <tr><td><? echo "<font size=2>第 
    $page 页,共 $MaxPage 页</font>";?></td><td></td></tr></table>
    <table align="center" width="80%" border="1" cellspacing="0" cellpadding="4" bordercolorlight="#CC9966" bgcolor="#00F2EE" bordercolordark="#FFFFFF" class="LZH">
    <tr bgcolor="#F7F2ff" style="font-size:14.8px;font-weight:bold">
    <?
    //显示表格头
    for($iCnt = 0; $iCnt < mssql_num_fields($rresult); $iCnt++)
    {
    echo "<td>".mssql_field_name($rresult,$iCnt)."</td>" ;
    }
    ?>
    </tr>
    <?
    //根据偏移量($page - 1)*$gPageSize,运用mssql_data_seek函数得到要显示的页面
    if( mssql_data_seek($rresult,($page-1)*$gPageSize) )

    $i=0;
    //循环显示当前纪录集
    for($i;$i<$gPageSize;$i++)
    {
    echo "<tr style=\"font-size:12px\">";
    //得到当前纪录,填充到数组$arr;
    $arr= mssql_fetch_row($rresult);
    if($arr)
    {
    //循环显示当前纪录的所有字段值
    for($nOffSet = 0;$nOffSet < count($arr);$nOffSet++)
    {
    echo "<td>".$arr[$nOffSet]."</td>";
    }
    }
    echo "</tr>";
    }
    }
    ?>
    </table>
    <br>
    <hr size=1 width=80%>
    <div align=center style="font-size:12px">
    <?
    //首页和上一页的链接
    if( $nNumRows>1 && $page>1)
    {
    $prevPage=$page-1; 
    echo " <a href=$PHP_SELF?page=1>首页</a> "; 
    echo " <a href=$PHP_SELF?page=$prevPage >上一页</a> ";
    }
    //下一页和末页的链接
    if( $page>=1 && $page<$MaxPage)
    {
    $nextPage= $page+1;
    echo " <a href=$PHP_SELF?page=$nextPage >下一页</a> ";
    echo " <a href=$PHP_SELF?page=$MaxPage >末页</a> "; 
    }
    ?>
    </div>
    </body>
    </html>
    把上述脚本程序进行一些修改,你就可以建立自己的服务器端分页显示的脚本程序了。
      

  3.   

    根据你的情况,你可能需要暂存首次查询结果。
    结果集不是很大的情况时可取结果集到数组用session保存
    结果集较大时可存于表中(不是临时表),注意表命的唯一性