页面id出现问题的分页

Pagination having issues with page id

本文关键字:分页 问题 id 页面      更新时间:2023-09-26

如果我在分页中的第=1页中有10个数据,另一个页面(即第=2页)有另外10个数据。在第=3页之后,该页面不包含任何数据。如果我选择上一个按钮,则该页面将进入第-1页、第=2页……如果我按下下一个按钮时,它也将进入第=2页、第+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> ";
      ?>

这个怎么样:

<?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;
      ?>

使用以下代码:

<?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版本。这将是今后的良好做法。

试试这个:

<?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> ";
?>