分页显示源程序:
<br><html><head><title>PHP分页</title></head><body><?//为了便于理解和更快地应用到工作中去,我们以MS SQL Server的NorthWind数据库Customers表为例。$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;//得到当前查询到的纪录数 $nNumRowsif(($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>  
自己来吧

解决方案 »

  1.   

    http://www.pconline.com.cn/pcedu/empolder/wz/php/10209/88055.html
      

  2.   

    include "db.inc.php";
      
    if(empty($keyword)){ //分页状态  
      $maxrows=10;
      $page=isset($page)?$page:1;//判断$page是否已设,如果设定,则$page=$page;否则,$page=1
      
      $str="SELECT COUNT(*) FROM zhongxin";
      $rescount=odbc_exec($odbc,$str);
      $productcount = odbc_result($rescount,1);
      //echo $productcount."<br><br>";//返回产品记录数
      //echo $rescount;//返回odbc的id值
      $pages=ceil($productcount/$maxrows);//求最小整数
      //echo $pages."<br>";
      //echo $page;
      
      if($page!=all&&$page!="new"){
        if($page<1){ $page=1; };
        if($page>$pages){ $page=$pages; };
    $pagestr="当前第".$page."页";
        $from=($page-1)*$maxrows;
    $product_disp=$from+$maxrows;
      }else{
    $from=0;  
        if($page==all){
            $pagestr="全部产品";
    $product_disp=$productcount;
      }else{
        $pagestr="最新产品";
    $product_disp=10;
      }
      }
      //echo $from." ".$product_disp;
      $str="SELECT TOP $product_disp * FROM zhongxin ORDER BY num";
      if($page=="new"){
        $str="SELECT TOP $product_disp * FROM zhongxin ORDER BY id DESC";  
      }
      //echo $str."<br>";
      $res=odbc_exec($odbc,$str);
      odbc_fetch_row($res,$from);  
      while(odbc_fetch_row($res)):
        $i++;
        $id[$i]=odbc_result($res,"id");
        $sign[$i]=odbc_result($res,"sign");
        $num[$i]=odbc_result($res,"num");
        $spec[$i]=odbc_result($res,"spec");
        $price[$i]=odbc_result($res,"price");
    //echo $i." ".$id[$i]." ".$sign[$i]." ".$num[$i]." ".$spec[$i]." ".$price[$i]."<br>";
      endwhile;
    }//-----------------------------------
    if(@$Search){
      $page=-1;
      $pagestr="查询结果";
      $from=0;
      $str="SELECT * FROM zhongxin WHERE (num like '%$keyword%') OR (sign like '%$keyword%') ORDER BY id DESC";
    //  echo $str."<br>";
      $res=odbc_exec($odbc,$str);
    //  odbc_fetch_row($res,$from);  
      while(odbc_fetch_row($res)):
        $i++;
        $id[$i]=odbc_result($res,"id");
    // echo $i." ".$id[$i]."<br>";
        $sign[$i]=odbc_result($res,"sign");
        $num[$i]=odbc_result($res,"num");
        $spec[$i]=odbc_result($res,"spec");
        $price[$i]=odbc_result($res,"price");
      endwhile;
      $product_disp=$i;  
    }
      include "top.php";
      
    ?><script LANUGAGE="JavaScript">
    <!--
    function openwindow(url,width,height) 
      {
    window.open(url,width,height,"toolbar=0,resizable=1,scrollbars,dependent,top=0,left=0");
      }
    function check() 

      if(document.form1.keyword.value=="") 
      { 
        alert("请输入查询内容!"); 
        document.form1.keyword.focus(); 
        return false; 
      }
    }
    //-->
    </script><link href="css.css" rel="stylesheet" type="text/css"><body>
    <table width="770" align="center">
      <tr>
        <td width="403" align="center"><a href="<? echo $PHP_SELF."?page=new"; ?>"><img src="images/new.gif" width="59" height="22" border="0"></a><a href="<? echo $PHP_SELF."?page=".($page-1); ?>"><img src="images/up.gif" width="59" height="22" border="0"></a><a href="<? echo $PHP_SELF."?page=".($page+1); ?>"><img src="images/down.gif" width="59" height="22" border="0"></a><a href="<? echo $PHP_SELF."?page=all"; ?>"><img src="images/all.gif" width="59" height="22" border="0"></a></td>
    <form name="form1" onsubmit="return check()" action="<? echo $PHP_SELF; ?>" method="post">     
      <td width="263" align="center">
        <input name="keyword" type="text" class="text" id="key" value="<? echo $keyword; ?>">
            <input name="Search" type="submit" class="button" value="查询">
    </td>
    </form>
        <td width="88" align="center"><strong>
          <?php echo $pagestr; ?></strong></td>
      </tr>
      <tr> 
        <td colspan="5">
    <?
      if(!empty($keyword)&&$product_disp==0) echo "<br><br><br>没有查询到相关纪录...<br><br><br>";
    ?>  
          <table width="98%" align="center" bgcolor="FFCC00" cellspacing="1">
    <?php 
      for($irow=0;$irow<ceil(($product_disp-$from)/5);$irow++){ 
    ?>
            <tr>
    <? for($icol=1;$icol<=5;$icol++){ $ii=$irow*5+$icol; ?>
              <td height="140" bgcolor="#FFFFFF" width="20%" align="center" valign="middle">
    <?php
        if($bfilename=get_image_file("image_big/17-".$id[$ii])){?>
          <a href="pop.php?id=<?echo $id[$ii];?>">
          <?php if(!$sfilename=get_image_file("image_small/17-".$id[$ii])){$sfilename="images/product_blank.gif";};   
      echo "<img src=$sfilename width=140 height=140 alt=产品-$num[$ii] border=0></a>";
            };
    };
    ?>
      </td>
            </tr>
            <tr> 
    <? for($icol=1;$icol<=5;$icol++){ $ii=$irow*5+$icol; ?>
              <td bgcolor="#FFFFFF">
    <?php
      if($bfilename=get_image_file("image_big/17-".$id[$ii])){
    //    echo $id[$ii]."<br>";
        echo "品牌:$sign[$ii]<br>";
        echo "产品编号:$num[$ii]<br>";
        echo "产品规格:$spec[$ii]<br>";
        echo "参考单价:$price[$ii]";
      }
    ?></td>
    <? }; ?>   
            </tr>
    <? }; ?>
          </table> 
        </td>
      </tr>
    </table>
    </body>