就是简单的数据分页,想知道那些url?page=1&id=2中page和id都是怎么找出来的,是菜鸟,麻烦了

解决方案 »

  1.   

    一般PAGE代表页数,ID代表信息在数据库中的标识。
    分页程序事实上都是这样工作的,:
      查出数据库中总信息条数,根据每页分多少条来确定总共有多少页,当前是第几页,得以类似于以下形式的东西  上一页 下一页 首页 末页 共几页,第几页 总信息条数:X条上一页就是PAGE-1
    下一页就是PAGE+1
    当然也不是这么简单,你还要判断是否大于了最大页数,或防止它变成0页
    然后再根据PAGE参数来定位记录,再显示多少条出来。
      

  2.   

    (PAGE-1)*每页多少条,决定本页应该应该从第几条信息开始显示,这样就将前面和后面的记录排 除了,也就是分页的形式了。
      

  3.   

    首先要统计一次总记录数,mysql数据库里用 limit 语句来返回指定页
      

  4.   

    如果在sql数据库中用什么函数可以返回指定页,就是11-20分一页,1-10分一页。
      

  5.   

    php_date_seek
    能不能具体一点啊,因为分页的文章都是以mysql为例的,有没有以mssql为例的程序能让我们学习一下啊
      

  6.   

    汗,忽然发现mssql里面没有limit这个参数,要用分页的存储过程来实现分页,大家有简单一点的mssql分页方法吗?
      

  7.   

    找到了一个不错的分页程序,但是不能翻页大家能不能帮我看看~~~!!!<?PHP
    extract($_GET);
    $conn=mssql_connect("localhost","sa","1" )  or die("Could not connect: " );
    mssql_Select_db("njc2",$conn);
    $strSQL="SELECT *  from rkgl";
    $result = mssql_query($strSQL);
    $total = mssql_num_rows($result);
    $pic= '<img src="images/real.gif">';
    $pagesize=10;
    if (($total%$pagesize)!=0) 
    $totalpage=intval($total/$pagesize)+1; 
    else 
    $totalpage=intval($total/$pagesize); 
    if ($page=="") 
    $current=1; 
    else{     
       switch($page){ 
        case "首页":  
        $current=1; 
        break; 
        case "上一页": 
        $current=$curpage-1; 
        break; 
        case "后一页": 
        $current=$curpage+1; 
        break; 
        case "尾页": 
        $current=$totalpage; 
        break;} 
        } 
        ?> 
        <b>第<?=$current;?>页,共<?=$totalpage;?>页.......共<?=$total;?>条记录  
     <?
    if($total<>0){ 
        mssql_data_seek($result,(($current-1)*$pagesize)); 

      } 
      $i=1; 
    ?>
    <table border="1" bgcolor="#F0FEE7" bordercolor="#336666"> 
    <? 
    while($row=mssql_fetch_row($result)) 
    {
    ?> 
          <tr> 
            <td><?=$row[0];?></td> 
            <td><?=$row[1];?></td> 
            <td><?=$row[2];?></td> 
            <td><?=$row[3];?></td> 
            <td><a href="play.php?id=<?=$row[0];?>"><?=$pic;?></a></td>
         </tr>
    <? 
        $i++; 
    if ($i>$pagesize) 
        break; 
    else 
        continue; 

    ?> 
    </table>
    <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'] ?> "> 
      <input type="hidden" name="curpage" value="<?echo $current;?>"> 
      <input type="submit" name="page" value="首页"> 
     <? if($current>1):?> 
      <input type="submit" name="page" value="上一页"> 
      <? endif; 
      if($current<>$totalpage):?> 
      <input type="submit" name="page" value="后一页"> 
      <? endif;?> 
      <input type="submit" name="page" value="尾页"> 
    </form>
      

  8.   

    我自己写的一个分页类,只是用来显示页码,不涉及数据库,如果想让数据库分页显示,只要通过传递的page参数和数据库中的总记录数算出当前页的起始位置,然后用select * from table limit m,n 就行了
    <?php
    //分页类(只用于显示页码http://wanghui.name)
    class page
    {
    var $pagesize=1; //每页显示的记录的最大值
    var $recordcount=0; //记录的总数
    var $pagecurrent=1; //当前页
    var $pagesegment=10; //每页显示的页码的个数的最大值
    var $pagecount; //总页数
    //**************************************************
    function page($p_size,$r_cont,$p_curr,$p_sgmt)
    {
    $this->set_size($p_size);
    $this->set_rcount($r_cont);
    $this->set_pcount($r_cont,$p_size);
    $this->set_current($p_curr);
    $this->set_segment($p_sgmt);
    }
    //**************************************************
    function set_size($p_size)
    {
    $this->pagesize=$p_size;
    }
    function set_rcount($r_cont)
    {
    $this->recordcount=$r_cont;
    }
    function set_current($p_curr)
    {
    if($p_curr<1)
    $this->pagecurrent=1;
    else
    {
    if($p_curr>$this->get_pcount())
    $this->pagecurrent=$this->get_pcount();
    else
    $this->pagecurrent=$p_curr;
    }
    }
    function set_segment($p_sgmt)
    {
    $this->pagesegment=$p_sgmt;
    }
    //总页数
    function set_pcount($r_cont,$p_size)
    {
    $this->pagecount=ceil($r_cont/$p_size);
    }
    //**************************************************
    function get_size()
    {
    return $this->pagesize;
    }
    function get_rcount()
    {
    return $this->recordcount;
    }
    function get_current()
    {
    return $this->pagecurrent;
    }
    function get_segment()
    {
    return $this->pagesegment;
    }
    function get_pcount()
    {
    return $this->pagecount;
    }
    //**************************************************
    function display()
    {
    //当前页码所属的页码段
    $sgmt_curr=intval(($this->get_current()-1)/$this->get_segment());
    //总页数
    $pagecount=$this->get_pcount();
    //当前页码段的起始页
    $p_from=$sgmt_curr*$this->get_segment()+1;
    //当前页码段的结束页
    $p_to=$sgmt_curr*$this->get_segment()+$this->get_segment();
    if($p_to>$pagecount) $p_to=$pagecount;
    //当前页URL
    $curr_url=$_SERVER["SCRIPT_NAME"];

    if($p_from>1)
    {
    $s_prev=$p_from-1;
    $p_prev=$this->get_current()-1;
    echo '<a href="'.$curr_url.'?page='.$s_prev.'">[<<]</a> ';
    echo '<a href="'.$curr_url.'?page='.$p_prev.'">[<]</a> ';
    }
    for($i=$p_from;$i<=$p_to;$i++)
    {
    if($i==$this->get_current())
    echo '<b>['.$i.']</b> ';
    else
    echo '<a href="'.$curr_url.'?page='.$i.'">['.$i.']</a> ';
    }
    if($p_to<$pagecount)
    {
    $s_last=$p_to+1;
    $p_last=$this->get_current()+1;
    echo '<a href="'.$curr_url.'?page='.$p_last.'">[>]</a> ';
    echo '<a href="'.$curr_url.'?page='.$s_last.'">[>>]</a>';
    }
    }
    }
    ?>可以这样进行测试
    <?php 
    require ("page.php");
    $page=$_GET['page'];

    //函数原型:function page($p_size,$r_cont,$p_curr,$p_sgmt)
    $mypage=new page(10,559,$page,10);
    $mypage->display();
    ?>
      

  9.   

    呵呵,老大你先帮我看下这个不能翻页的分页程序错在哪里,能显示10条数据,就是不能翻页。<?PHP
    extract($_GET);
    $conn=mssql_connect("localhost","sa","1" )  or die("Could not connect: " );
    mssql_Select_db("njc2",$conn);
    $strSQL="SELECT *  from rkgl";
    $result = mssql_query($strSQL);
    $total = mssql_num_rows($result);
    $pic= '<img src="images/real.gif">';
    $pagesize=10;
    if (($total%$pagesize)!=0) 
    $totalpage=intval($total/$pagesize)+1; 
    else 
    $totalpage=intval($total/$pagesize); 
    if ($page=="") 
    $current=1; 
    else{     
       switch($page){ 
        case "首页":  
        $current=1; 
        break; 
        case "上一页": 
        $current=$curpage-1; 
        break; 
        case "后一页": 
        $current=$curpage+1; 
        break; 
        case "尾页": 
        $current=$totalpage; 
        break;} 
        } 
        ?> 
        <b>第<?=$current;?>页,共<?=$totalpage;?>页.......共<?=$total;?>条记录  
     <?
    if($total<>0){ 
        mssql_data_seek($result,(($current-1)*$pagesize)); 

      } 
      $i=1; 
    ?>
    <table border="1" bgcolor="#F0FEE7" bordercolor="#336666"> 
    <? 
    while($row=mssql_fetch_row($result)) 
    {
    ?> 
          <tr> 
            <td><?=$row[0];?></td> 
            <td><?=$row[1];?></td> 
            <td><?=$row[2];?></td> 
            <td><?=$row[3];?></td> 
            <td><a href="play.php?id=<?=$row[0];?>"><?=$pic;?></a></td>
         </tr>
    <? 
        $i++; 
    if ($i>$pagesize) 
        break; 
    else 
        continue; 

    ?> 
    </table>
    <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'] ?> "> 
      <input type="hidden" name="curpage" value="<?echo $current;?>"> 
      <input type="submit" name="page" value="首页"> 
     <? if($current>1):?> 
      <input type="submit" name="page" value="上一页"> 
      <? endif; 
      if($current<>$totalpage):?> 
      <input type="submit" name="page" value="后一页"> 
      <? endif;?> 
      <input type="submit" name="page" value="尾页"> 
    </form>
      

  10.   

    据说是不用limit 就翻不了页了,汗,可是mssql中没有limit,难道真的要用存储过程来分页吗,大家看看还有其他简单的方法吗?
      

  11.   

    To:littlejc2(半半)
    在我的帖子中的mssql_data_seek()是没有问题的。
    作用是用来指定N页后开始显示的第一条记录指针位置。PS:
    我结合原来的代码,自己写了一个。下面是代码,我已经测试成功。希望对你有所帮助。 :)mysql_connect("localhost","root","");
             $result=mysql_db_query("MsgBoard","select * from message");
             $total=mysql_num_rows($result);
     $pagesize=10;
             if (($total%$pagesize)!=0) 
             $totalpage=intval($total/$pagesize)+1; 
             else 
             $totalpage=intval($total/$pagesize); 
     if (empty($_GET['pagenum'])){
     $_GET['pagenum']=1; }
     if($total!=0){ 
               mysql_data_seek($result,(($_GET['pagenum']-1)*$pagesize)); 
                          } 
                     $i=1; 
       
       while($row=mysql_fetch_row($result)) 
       {
       echo "<tr>";
               echo "<td width=51%><div align=center>"."<a href=DisplayTopic.php?id=".$row["0"].">".$row["5"]."</a>"."</div></td>";
               echo "<td width=14%><div align=center>"."<a href=DisplayUserinfo.php?name=".$row["2"].">".$row["2"]."</a>"."</div></td>";
               echo "<td width=21%><div align=center>".$row["3"]."</div></td>";
               echo "<td width=14%><div align=center>"."<a href=DisplayUserinfo.php?name=".$row["2"].">".$row["6"]."</a>"."</div></td>";
               echo "</tr>";
      
     
      $i++; 
         if ($i>$pagesize) 
        break; 

         else 
          continue; 
             }
        ?>
        </table><br><?
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=1>首页</a> ";
      if ($_GET['pagenum']>1){
      $temp_current=$_GET['pagenum']-1;
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">上一页</a> "; }
      if ($_GET['pagenum']<$totalpage){
      $temp_current=$_GET['pagenum']+1;
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">下一页</a> "; }
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$totalpage.">尾页</a> ";
      

  12.   

    #初始化翻页变量
    function initpage(){
    #初始化$this->page为数组,存储方式为
    #page[0]=当前是第几页
    #page[1]=每页显示几条数据
    #page[2]=总共有多少条数据
    #page[3]=从哪条数据开始读取
    #page[4]=总共有多少页
    #php&mysql分页方法:
    #select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize
    #初始化
    $pg=$_GET['page'];
    #取得页数
    $this->page[0]=(isset($pg)?intval($pg):1);
    #每页显示几条数据,默认显示20条每页
    $this->page[1]=(DEFINED("DEF_PAGE_SIZE")?DEF_PAGE_SIZE:20);
    #总共有多少条数据
    #echo $this->pastsql[1][$this->pastsql[0][0]];
    $this->executeA($this->pastsql[0][0]);
    $this->page[2]=$this->rs->RecordCount();
    #设置开始读取数据ID
    $this->page[3]=(($this->page[0]-1)*$this->page[1]);
    #总共页数
    if($this->page[2]){
       #总数据量小于$PageSize,只有一页
       if( $this->page[2]<$this->page[1]){$this->page[4]= 1;}  
       #取总数据量除以每页数的余数
       if( $this->page[2]%$this->page[1]){ 
       #如果有余数,则页数等于总数据量除以每页数的结果取整再加一                        
       $this->page[4]= (int)($this->page[2]/$this->page[1]) + 1;   
       #如果没有余数,则页数等于总数据量除以每页数的结果
       }else{
       $this->page[4] = $this->page[2] / $this->page[1];                      
       }
    }
    #没有数据的时候置0
    else{
       $this->page[4] = 0;
    }
    }
      

  13.   

    <?php
    /*
     ********************************************************************
     *  Editor           :  EditPlus+ 2.11                              *
     ******************************************************************** +------------------------------------------------------------------+
     |  PHP Version 4                                                   |
     +------------------------------------------------------------------+
     |  Copyright (C) 2004-2005 All Right Reseved Pc Rookie's           |
     +------------------------------------------------------------------+
     |  Notices:                                                        |
     |  If you found any error in the source code. Then I'll hope you s-|
     |end Bug email to us. So we'll thank you very much.                |
     |                                                                  |
     |                                                                  |
     |                                                                  |
     +------------------------------------------------------------------+
     |  Website         :   http://www.5down6.net                       |
     |  Author          :   Pc Rookie's                                 |
     |  Contact Email   :   [email protected]                            |
     |  LastModify      :   2004/11/12                                  |
     +------------------------------------------------------------------+
     +------------------------------------------------------------------+
     |  FileName        :   DB.php                                      |
     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     |  Function        :   Database independent query interface        |
     +------------------------------------------------------------------+
    */class DB
    {
    var $Host        = "";   //数据库主机
    var $Database    = "";   //数据库
    var $User        = "";   //用户名
    var $Password    = "";   //密码 var $Auto_Free   = 0;    //设置为1程序结束会执行mysql_free_result()
    var $Debug       = 1;    //设置为0不显示调试信息
    var $Halt_On_Err = "yes";/*设置为"yes"终止错误并显示信息;
                               设置为"no"忽略错误;
       设置为"report"忽略错误,但是会报告错误.
                             */
    var $PConnect    = 0;    //设置为1数据库将使用mysql_pconnect持久连接

    var $linkID      = 0;    //链接标识
    var $queryID     = 0;    //查询结果标识 var $queryRow    = 0;    //SQL影响的行 //数据库链接方法
        /*
      返回值:
             链接资源标识
    */
    function DB($Host = "", $User = "", $Password = "", $Database = "")
    {
    //检查各项参数是否为空
    if(!($this -> Host == "") && !($this -> User == "") && !($this -> Database== ""))
    {
    //不为空!
    $Host     = $this -> Host;
    $User     = $this -> User;
    $Password = $this -> Password;
    $Database = $this -> Database;
    }
    else
    {
    //为空!子类定义的参数不完整
    die(SUB_CLASS_PARAMETER_IS_NOT_FULL);
    }

    //成功链接到数据库否
    if($this -> linkID == 0)
    {
    //尚未链接到数据库!链接方式选择持久链接还是普通链接
    if($this -> PConnect)
    {
    //持久链接
    $this -> linkID = @mysql_pconnect($Host, $User, $Password);
    }
    else
    {
    //普通链接
    $this -> linkID = @mysql_connect($Host, $User, $Password);
    }

    //成功链接到数据库否
    if(!$this -> linkID)
    {
    //链接失败!输出错误信息
    DB::mysql_err_msg();
    }
    else
    {
    //链接服务器成功!打开数据库失败否
    if(!@mysql_select_db($this -> Database))
    {
    //数据库不存在则建立
    if(!@mysql_query("create database ".$this -> Database));
    {
    //打开数据库失败!输出错误信息
    DB::mysql_err_msg();
    }
    }
    }
    }
    //返回成功的链接标识
    return $this -> linkID;
    } //SQL语句执行方法
    /*
      返回值:
             SQL语句影响的总行数
             SQL对任何行没产生影响则返回字符串
    */
    function query($SQL = "")
    {
    //SQL语句为空否
    if($SQL == "")
    //为空!SQL语句为空
    die(SQL_ERR_MSG_SQL_IS_EMPTY);
    else
    {
    //不为空!执行SQL并将结果标识赋给$queryID
    $this -> queryID = @mysql_query($SQL);
    //SQL执行成功否
    if(!$this -> queryID)
    {
    //失败!输出错误信息
    DB::mysql_err_msg();
    }
    else
    {
    //成功!是SELECT语句否
    if(eregi("select",$SQL))
    {
    //是(SELECT)
    $this -> queryRow = @mysql_num_rows($this -> queryID);
    }
    else
    {
    //不是(是INSERT、DELETE、UPDATE)
    $this -> queryRow = @mysql_affected_rows();
    }
    //响应SQL语句的记录数为0否
    if(!($this -> queryRow))
    //返回无记录响应提示信息
    return $this -> queryRow = NOT_ANY_REC;
    }
    }
    } //获取查询操作结果集
    /*
      返回值: 
             数组Key为数据库字段名的二维数组
    */
    function rs()
    {
    //查询结果记录集为0否
    if($this -> queryRow)
    {
    //结果不为0;有符合条件记录
    //按行生成记录结果集
    $result =  mysql_fetch_object($this -> queryID);
    //按照查询结果记录集的字段总数
    for($j = 0; $j < mysql_num_fields($this -> queryID); $j++)
    {
    //依次获取字段名存入数组
    $fields = mysql_field_name($this -> queryID, $j);
    //按照[Key] => [Fields]、[Value] => [Value]赋值
    $rs[$fields] = $result -> $fields;
    }
    //返回数组
    return $rs;
    }
    } //获取分页参数的方法
    /*
      返回值:
             数组(SQL影响记录数, 定义每页显示记录数, 当前页, 总页数, 每页开始标记, 每页结束标记)
     使用:list函数获[推荐]
          list($total_rec, $page_size, $page, $total_page, $page_start, $page_end) = $obj -> splitPage($page_size, $page);   直接获取[不推荐]
    */
    function splitPage($page_size = 15,$page = 1)
    {
    //判断当前页面为第一页否
    $page ? $page = $page : $page = 1;
    //判断总记录数超过一页否
    if($page_size > $this -> queryRow)
    {
    //记录不多于一页,设置总页数
    $total_page = 1;
    //当前页起始标记值
    $page_start = 0;
    //当前页结束标记值
    $page_end = $this -> queryRow;
    }
    else
    {
    //记录总数多于一页,计算总页数
    $total_page = ceil($this -> queryRow/$page_size);
    //当前页起始标记值
    $page_start = $page_size*($page-1);
    //当前页结束标记值
    $page_end = $page_size*$page;
    //当前页为末页否
    if($page_end > $this -> queryRow)
    //为末页,更改当前页结束标记值为总记录数
    $page_end  = $this -> queryRow;
    }
    //定位数据库指针
    @mysql_data_seek($this -> queryID, $page_end);
    //返回数组
    return $split = array($this -> queryRow, $page_size, $page, $total_page, $page_start, $page_end);
    } //释放资源方法
    function free()
    {
    @mysql_free_result($this ->queryID);
    $this -> queryID = 0;
    } //错误处理方法
    function mysql_err_msg()
        {
    //获取错误代码
    $this -> errno = @mysql_errno();
    //获取错误信息
    $this -> error = @mysql_error(); //输出错误代码、信息
    $this -> print_mysql_error();
        } //错误信息输出方法[返回字符串]
    function print_mysql_error()
    {

    if($this -> Debug)
    {
    switch($this -> Halt_On_Err)
    {
    case("yes"):
    //终止程序执行并输出错误代码、信息
    die($this -> errno.":".$this -> error);
    break;
    case("no"):
    break;
    default:
    //输出错误代码、信息
    echo $this -> errno.":".$this -> error;
    break;
    }
    }
    }
    ?>
    里面有获取分页参数的类自己看看吧
      

  14.   

    感谢solo_php(晕),也谢谢其他各位,确实把mssql的分页搞定了,solo_php(晕) 的那个程序对于所有的数据库分页都是可行的,大家可以参照一下,另外路过的大哥们看看那个分页还有什么bug吗,最好把这个弄出个摸板来,就最好了
      

  15.   

    回复人: littlejc2(半半) ( ) 信誉:100 谢谢.........研究完成....
      

  16.   

    <?php echo "<?xml version=\"1.0\" encoding=\"gb2312\"?".">"; ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    </head>
    <body>
    <?PHP
    $conn=mssql_connect("localhost","sa","1" )  or die("Could not connect: " );
    mssql_Select_db("njc2",$conn);
    $strSQL="SELECT *  from rkgl";
    $result = mssql_query($strSQL);
    $total = mssql_num_rows($result);
    $pagesize=10;
    if (($total%$pagesize)!=0) 
    $totalpage=intval($total/$pagesize)+1; 
    else 
    $totalpage=intval($total/$pagesize); 
    if (empty($_GET['pagenum'])){
     $_GET['pagenum']=1; }
      if($total!=0){ 
               mssql_data_seek($result,(($_GET['pagenum']-1)*$pagesize)); 
                          } 
                     $i=1; 
     ?>
    <table border="0" > 
    <?PHP
    while($row=mssql_fetch_row($result)) 
       {?>
          <tr> 
            <td><?=$row[0];?></td> 
            <td><?=$row[1];?></td> 
            <td><?=$row[2];?></td> 
            <td><?=$row[3];?></td> 
    <td><?=$row[4];?></td> 
           <td><a href="play.php?id=<?=$row[0];?>"><?=$row[3];?></a></td>
         </tr>
     <? $i++; 
         if ($i>$pagesize) 
        break; 
         else 
          continue; 
             }
        ?>
        <b>第<?=$_GET['pagenum'];?>页,共<?=$totalpage;?>页.......共<?=$total;?>条记录
    </table><br/>
    <?PHP
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=1>首页</a> ";
      if ($_GET['pagenum']>1){
      $temp_current=$_GET['pagenum']-1;
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">上一页</a> "; }
      if ($_GET['pagenum']<$totalpage){
      $temp_current=$_GET['pagenum']+1;
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$temp_current.">下一页</a> "; }
      echo "<a href=".$_SERVER['PHP_SELF']."?pagenum=".$totalpage.">尾页</a> ";
    ?>
    </body>
    </html>
    这是我改进后的mssql的一个通用分页方法,只要把数据库名字改下就行了,大家看看有什么错误吗?