0

您好,我的分页系统遇到问题,如果我列出来自 mysql 表的结果,它工作正常,但如果我在 SQL 查询中添加一些条件,如“AND”此列“AND”其他列脚本在第一页上正确显示结果,当我选择第二页而不是从 26 向前显示结果的第二部分时,它正在开始一个新的分页,它从一开始就显示所有内容,而没有在查询中添加条件。这是带有查询的分页代码:

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

?>

这是生成的页面链接的代码:

<?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           
          }
        ?>
4

3 回答 3

1

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           
          }
        ?>
于 2013-10-30T22:41:05.800 回答
1

如果 $ciudad 和 $tipo 都不是空的,您的执行查询将如下所示:

SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' ORDER BY id DESC AND tipo= '$tipo' ORDER BY id DESC 

如果我没记错的话应该是这样的:

SELECT * FROM cursos WHERE 1 AND ciudad = '$ciudad' AND tipo= '$tipo' ORDER BY id DESC

我要做的是改变这个:

$sql = "SELECT * FROM cursos WHERE 1";
if (!empty($ciudad)) {
  $sql .= " AND ciudad = '$ciudad' ORDER BY id DESC ";
}
if (!empty($tipo)) {
  $sql .= " AND tipo= '$tipo' ORDER BY id DESC ";
}

这也是:

$sql = "SELECT * FROM cursos WHERE 1 ";
if (!empty($ciudad)) {
    $sql .= "AND ciudad= '$ciudad' ";
    if (!empty($tipo)) {
       $sql .= "AND tipo= '$tipo' ";
    }

      $sql .= "ORDER BY id DESC ";
    }

我还有一个链接,可以帮助您进行分页。

http://www.phpjabbers.com/php--mysql-select-data-and-split-on-pages-php25.html

于 2013-10-30T22:37:34.843 回答
0

如果设置了城市和类型,那么您的 SQL 将有两个 order by 实例...您应该在 if 语句之后添加 order by。

于 2013-10-30T22:34:35.723 回答