dongyi1777 2013-10-30 22:23
浏览 35
已采纳

当添加不同的sql查询条件时,Pagination Php不起作用

Hello I am facing a problem with my pagination system, where if I list results from a mysql table it is working fine, but in case If I add some conditions inside the SQL Query like "AND" this column "AND" other column the script shows the results properly on the first page, when I chooce the second page instead of showing the second portion of results from 26 forward it is starting a new pagination and it is showing everything from the begining without the contions added inside the query. Here is the code of the pagination with the query:

 //This gets all the other information from the form 
 $ciudad=$_POST['ciudad']; 
 $tipo=$_POST['tipo']; 

$con=mysqli_connect();
// Check connection
$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
  $sql .= " AND ciudad = '$ciudad' ";
}
if (!empty($tipo)) {
  $sql .= " AND tipo= '$tipo' ";
}
if (!$result = mysqli_query($con,$sql))
{
    die("Error: " . mysqli_error($con));
}

$per_page =25;//define how many games for a page
$count = mysqli_num_rows($result);
$pages = ceil($count/$per_page);

if(!isset($_GET['page']) || $_GET['page']=="") {
  $page="1";
} else {
  $page=$_GET['page'];
}
$start    = ($page - 1) * $per_page;
$sql = "SELECT * FROM cursos WHERE 1 LIMIT $start,$per_page";

?>

This is the code of the generated pages links:

<?php
        //Show page links
        for ($i = 1; $i <= $pages; $i++)
          {?>
          <li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i;?>"><?php echo $i;?></a></li>
          <?php           
          }
        ?>
  • 写回答

3条回答 默认 最新

  • dongshang4984 2013-10-30 22:41
    关注

    The 2 problems where:

    • additional filter are not anymore selected in the next page ($_POST will be empty)
    • instructions related to pagination where calculated AFTER the query (which, obviously, couldn't use theses parameters)

    You can either store your extra queries conditions in session, or add it as parameter in the "next page link", or transform your link to a submit form (which is probably the best option)

    <li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i.'&amp;ciudad='.$ciudad.'&amp;tipo='.$tipo; ?>"><?php echo $i;?></a></li>
    

    If you choose the link solution, don't forget to change your _POST in _GET (or check the second if the first is empty, or use $_REQUEST)

    I have to mention your code is not sql injection free and using mysqli_prepare() may worth the time (for security and performances)

    EDIT: so, here we go:

    sidenotes: using $_REQUEST is not always recommended

    And I noticed also you execute your query BEFORE using the pagination system...

     //This gets all the other information from the form 
     $ciudad=$_REQUEST['ciudad']; 
     $tipo=$_REQUEST['tipo']; 
    
    $con=mysqli_connect();
    // Check connection
    $sql = "SELECT * FROM cursos WHERE 1";
    if (!empty($ciudad)) {
      $sql .= " AND ciudad = '$ciudad' ";
    }
    if (!empty($tipo)) {
      $sql .= " AND tipo= '$tipo' ";
    }
    
    //  PAGINATION MOVED UP
    
    $per_page =25;//define how many games for a page
    $count = mysqli_num_rows($result);
    $pages = ceil($count/$per_page);
    
    if(empty($_GET['page'])) {
      $page="1";
    } else {
      $page=$_GET['page'];
    }
    $start    = ($page - 1) * $per_page;
    
    $sql .= ' LIMIT '.$start.','.$per_page;
    
    if (!$result = mysqli_query($con,$sql))
    {
        die("Error: " . mysqli_error($con));
    }
    
    
    
            //Show page links
            for ($i = 1; $i <= $pages; $i++)
              {?>
              <li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i.'&amp;ciudad='.$ciudad.'&amp;tipo='.$tipo; ?>"><?php echo $i;?></a></li>
              <?php           
              }
            ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)