搜索结果的php分页无法显示
我刚刚在学习分页。我很难让它用于搜索结果。它以正确的链接数正确显示第一页,但即使在第1页上单击任何链接,也会进入空白页 谁能告诉我我做错了什么 以下代码用于单击“搜索”按钮时使用搜索结果的php分页无法显示,php,search,pagination,Php,Search,Pagination,我刚刚在学习分页。我很难让它用于搜索结果。它以正确的链接数正确显示第一页,但即使在第1页上单击任何链接,也会进入空白页 谁能告诉我我做错了什么 以下代码用于单击“搜索”按钮时使用 <?php include('includes/connect-db.php'); include('includes/functions.php'); if (isset($_GET['searchbtn'])){ $product=$_GET['products']; $status=$_
<?php
include('includes/connect-db.php');
include('includes/functions.php');
if (isset($_GET['searchbtn'])){
$product=$_GET['products'];
$status=$_GET['ticket_status'];
$order_by=$_GET['order_by'];
$ticket_type=$_GET['ticket_type'];
#check if product has been selected
if ($_GET['products'] == 'select'){
echo '<font class="error">  Please select a product to search.</font>';
}else{
if ($status == 'All' AND $order_by == 'All' AND $ticket_type == 'All' ){
$page_query="SELECT * FROM tickets WHERE product='$product' ORDER BY created DESC";
}elseif ($ticket_type == 'BOX'){
$page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND pbi <> '-' ORDER BY '$order_by' ";
}elseif ($ticket_type == 'PVR'){
$page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND inc <> '-' ORDER BY '$order_by' ";
}else{
$page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' ORDER BY created DESC";
}
}#end of else
$results_per_page=3;
$result_set=pagination($results_per_page,$page_query);
$query=$result_set['query'];
$pages=$result_set['pages'];
#SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset. A resource on success or False on Error
if (!empty($query)){
$result = mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());
$num_results = mysqli_num_rows($result);
if ($num_results > 0){
displayTicket($result);
if ($pages > 1){
echo '</br>';
for($i=1;$i<=$pages;$i++){
echo ' <a href="?page='.$i.'">'.$i.'</a> ';
}
}
}else{
echo "  No Records found.";
}
}#query string is not empty
}
?>
注意:仅当我尝试使用搜索功能时,链接到其他页面的分页才会失败。我曾在一个页面上尝试过它,该页面在表中列出了信息,链接工作正常。在我看来,问题在于链接中的信息:localhost/test/searchProductForm.php?page=1告诉searchProductForm显示哪个页面,但没有告诉它搜索信息是什么。否则,您的搜索页面将不知道显示什么。尝试更改分页链接以包含原始搜索链接中的所有信息,即您的产品、票证状态、订购人和票证类型参数 我知道这是一个旧线程,但是对于任何不能用上面使用的代码解决类似问题的人来说,这可能是由于文档开头缺少session\u start
我之所以这样说,是因为尽管代码试图获取通过?page=&products=etc传递给$\u GET语句的会话变量,但它们并没有从URL检索到变量中,但您需要将会话从PHP文档的请求处开始,以将信息从URL传递到字符串。同样在您的函数$pagination[]=数组;应为$pagination=array;否则,它就违背了发起它的目的`这个空白页的URL是什么?你能得到空白页来张贴关于它接收到的参数的任何信息吗?@ Octnn:当我第一次运行搜索时,我得到:当我点击第1页或第2页:@劳伦斯切龙:谢谢你的提示!请停止使用古老的mysql_*函数编写新代码。它们不再得到维护,社区已开始恢复。相反,你应该学习并使用或。如果你不能决定,将帮助你做出选择。如果你想学习,谢谢。我做了以下操作,它似乎保留了我在点击页码时选择的选项,但仍然没有显示结果。回响现在,当我单击第2页时,例如:http://localhost/test/searchProductForm.php?page=2&products=Box&tic??ket_status=All&order_by=All&ticket_type=All 我为每页的页面、开始和结果设置了echo,当我运行搜索时,我得到:第1页每页开始0,但当我单击第1页或第2页链接时,它不回显。你能让它在页面上回显任何内容吗?如果没有,那么页面可能会消亡。您是否已将其设置为显示错误,或者是否已检查错误日志?如果没有,请尝试在页面开头添加ini_set'display_errors',1行,看看这是否说明了什么。谢谢。做了那件事,什么也没做。仍然显示一个空页面,其中我的选择完好无损。也许我需要构建一个简单的搜索页面并对其进行测试..?您不需要会话来访问$\u GET变量…仅当您想访问$\u会话变量时
function pagination($per_page, $pages_query){
include('includes/connect-db.php');
$pagination[]=array();
#total result count
$total_result=mysqli_query($db,$pages_query);
#ceil takes a decimal number and gives the nearest whole number
$total=mysqli_num_rows($total_result);
$pages = ceil($total / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;
$query = " LIMIT $start,$per_page";
$query = $pages_query.$query;
$pagination['query'] = $query;
$pagination['pages'] = $pages;
return $pagination;
}