【问题标题】:Pagination having issues with page id分页存在页面 id 问题
【发布时间】:2013-10-15 18:48:13
【问题描述】:

如果我在分页中有 10 个数据 = 1 页,另一个页面有,即页面 = 2 有另外 10 个数据。并且在该页面 = 3 之后不包含任何数据。如果我选​​择上一个按钮,则该页面进入page-1,page=-2 .....如果我按下下一个按钮,它也会像 page=2,page=3 等那样继续。所以如果记录没有,如何禁用上一个和下一个按钮更长的可用时间?

<?php
    include("config.php"); 
    $start = 0;
    $per_page = 5;
    if(!isset($_GET['page'])){
        $page = 1;
    } else{
        $page = $_GET['page'];
    }
    if($page<=1)
        $start = 0;
    else
        $start = $page * $per_page - $per_page;
        $sql="select id,question,correctAnswer,category from math order by id";
        $num_rows = mysql_num_rows(mysql_query($sql));
        $num_pages = $num_rows / $per_page;
        $sql .= " LIMIT $start, $per_page";
        $result=mysql_query($sql) or die(mysql_error());
        while($row=mysql_fetch_array($result))                  
        { ?>
         .......            
         ........
      <?php
      $prev = $page - 1;
      $next = $page + 1;
      echo "<a href='?page=$prev'>prev</a> ";
      echo " <a href='?page=$next'>next</a> ";
      ?>

【问题讨论】:

  • 提示:您应该验证并转义所有用户输入以避免注入。还要检查那些 mysql_* 函数的手册,因为它们已被弃用。

标签: php javascript pagination


【解决方案1】:

这个怎么样:

<?php
    include("config.php"); 
    $start = 0;
    $per_page = 5;
    if(!isset($_GET['page'])){
        $page = 1;
    } else{
        $page = $_GET['page'];
    }
    if($page<=1)
        $start = 0;
    else
        $start = $page * $per_page - $per_page;
        $sql="select id,question,correctAnswer,category from math order by id";
        $num_rows = mysql_num_rows(mysql_query($sql));
        $num_pages = $num_rows / $per_page;
        $sql .= " LIMIT $start, $per_page";
        $result=mysql_query($sql) or die(mysql_error());
        while($row=mysql_fetch_array($result))                  
        { ?>
         .......            
         ........
      <?php

      if($page > 1){
         $prev = $page - 1;
         $prev = " <a href='?page=$prev'>prev</a> ";
      } else {
         $prev = "";
      }
      if($page < $num_pages){
         $next = $page + 1;
         $next = " <a href='?page=$next'>next</a> ";
      }
      echo $prev;
      echo $next;
      ?>

【讨论】:

  • 另外,更改 $num_pages = $num_rows / $per_page; to $num_pages = floor($num_rows / $per_page) 这将四舍五入所有奇数。
【解决方案2】:

使用下面的代码:

<?php
    include("config.php"); 
    $start = 0;
    $per_page = 5;
    if(!isset($_GET['page'])){
        $page = 1;
    } else{
        $page = $_GET['page'];
    }
    if($page<=1)
        $start = 0;
    else
        /*add these lines*/
        $cnt=mysql_query("select count(*) as ct from math") or die(mysql_error());
        $data=mysql_fetch_array($cnt);
        $total = $data['ct'];
        $start = $page * $per_page - $per_page;
        $sql="select id,question,correctAnswer,category from math order by id";
        $num_rows = mysql_num_rows(mysql_query($sql));
        $num_pages = $num_rows / $per_page;
        $sql .= " LIMIT $start, $per_page";
        $result=mysql_query($sql) or die(mysql_error());
        while($row=mysql_fetch_array($result))                  
        { ?>
         .......            
         ........
      <?php
      $prev = $page - 1;
      $next = $page + 1;
      /*also add these condition*/
      if($page > 1)
           echo "<a href='?page=$prev'>prev</a> ";
      /*also add these condition*/
      if($total > ($page*$per_page))
           echo " <a href='?page=$next'>next</a> ";
      ?>

注意:始终使用 PDO 或 mysqli_*,因为 mysql_* 在 最新的 PHP 版本。这将是未来的好习惯。

【讨论】:

    【解决方案3】:

    试试这个:

    <?php
    include("config.php"); 
    $per_page = 5;
    $page = (is_numeric($_GET['page']) ? $_GET['page'] : 0);
    $sql = "SELECT `id`,`question`,`correctAnswer`,`category` FROM `math` ORDER BY `id`";
    $num_rows = mysql_num_rows(mysql_query($sql));
    $num_pages = ceil($num_rows / $per_page);
    $sql .= " LIMIT ".$per_page*$page.", {$per_page}";
    $result=mysql_query($sql) or die(mysql_error());
    while ($row=mysql_fetch_array($result)) { ?>
    .......           
    ........
    <?php
    $prev = $page - 1;
    $next = $page + 1;
    echo "<a href='?page=$prev'>prev</a> ";
    echo " <a href='?page=$next'>next</a> ";
    ?>
    

    【讨论】:

    • 谢谢 Nils 的 rpl..但它给了我这样的错误:你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '-5, 5' 附近使用正确的语法
    • 你确定你使用的是这个确切的代码吗?原因 $per_page 因为它是 5,而 $page 默认是 0 — 5*0=0,它不能创建 -5。不过,尝试替换 $page = (is_numeric($_GET['page']) ? $_GET['page'] : 0);到 $page = (is_numeric($_GET['page']) && > 0 ? $_GET['page'] : 0);
    猜你喜欢
    • 2011-08-01
    • 2020-10-15
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多