您好,我的分页系统遇到了问题,如果我列出 mysql 表中的结果,它可以正常工作,但是万一如果我在 SQL 查询中添加一些条件,例如"AND"此列"AND"其他列,脚本会在第一页上正确显示结果,当我选择第二页而不是显示 26 向前的结果的第二部分时,它正在开始新的分页它从一开始就显示所有内容,而无需在查询中添加 contions。下面是带有查询的分页代码:
//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
}
?>
其中的 2 个问题:
- 下一页中不再选择其他过滤器(
$_POST
将为空) - 与分页相关的指令,在查询后计算(显然,不能使用这些参数)
您可以将额外的查询条件存储在会话中,或将其作为参数添加到"下一页链接"中,或者将您的链接转换为提交表单(这可能是最佳选择)
<li id="<?php echo $i;?>"><a href="search_cursos.php?page=<?php echo $i.'&ciudad='.$ciudad.'&tipo='.$tipo; ?>"><?php echo $i;?></a></li>
如果您选择链接解决方案,请不要忘记在_GET
中更改_POST
(或者如果第一个为空,请检查第二个,或使用$_REQUEST
)
我不得不提一下,你的代码不是sql注入自由的,使用mysqli_prepare()可能值得花时间(为了安全性和性能)
编辑:所以,我们来了:
旁注:并不总是建议使用 $_REQUEST
我还注意到您在使用分页系统之前执行查询......
//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.'&ciudad='.$ciudad.'&tipo='.$tipo; ?>"><?php echo $i;?></a></li>
<?php
}
?>
如果 $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
如果设置了城市和类型,那么您的 SQL 将有两个按...您应该在 if 语句后添加顺序。