分页有页面ID的问题

时间:2013-08-28 07:05:51

标签: php javascript pagination

如果我在页面中有10个数据= 1在分页中,另一个页面具有ie = 2有另外10个数据。在该页面= 3之后不包含任何数据。如果我选​​择上一个按钮然后该页面进入page-1,page = -2 .....如果我按下一个按钮,它也将继续像page = 2,page = 3等等。如果记录为no,如何禁用prev和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
        $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> ";
      ?>

3 个答案:

答案 0 :(得分: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;
      ?>

答案 1 :(得分: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版本。这将是未来的良好实践。

答案 2 :(得分: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> ";
?>