3

如果我在分页中有 10 个数据 = 1,另一个页面有 ie = 2 有另外 10 个数据。并且在该页面 = 3 之后不包含任何数据。如果我选​​择上一个按钮,那么该页面进入第 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> ";
      ?>
4

3 回答 3

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
        $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;
      ?>
于 2013-08-28T07:15:36.677 回答
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
        /*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 版本中已弃用。这将是未来的好习惯。

于 2013-08-28T07:17:59.897 回答
0

尝试这个:

<?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> ";
?>
于 2013-08-28T07:15:16.130 回答