美好的一天。我需要你的专家意见。我一直在使用来自mysql的过滤结果进行分页。
过滤后的结果仅适用于第一页。单击下一页或后续页面,我将收到以下错误,并显示数据库中的所有数据。
注意:未定义的索引:搜索注意:未定义的索引:位置 注意:未定义的索引:dept注意:未定义的索引:bldg 等等...
我想知道我可能会错过什么......
<?php
include('include/connect.php');
$tableName="employee";
$targetpage = "searchresults.php";
$limit = 5;
$search = mysql_real_escape_string($_GET['search']);
$position = mysql_real_escape_string($_GET['position']);
$dept = mysql_real_escape_string($_GET['dept']);
$bldg = mysql_real_escape_string($_GET['bldg']);
$query = "SELECT COUNT(*) as num FROM $tableName WHERE name like '%{$search}%' AND position like '%{$position}%' AND dept like '%{$dept}%' AND bldg like '%{$bldg}%'";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];
$stages = 3;
// $page = mysql_escape_string($_GET['page']);
$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 0;
if($page){
$start = ($page - 1) * $limit;
}else {
$start = 0;
}
// Get page data
$query1 = "SELECT * FROM $tableName WHERE ads like '%{$search}%' AND position like '%{$position}%' AND dept like '%{$dept}%' AND bldg like '%{$bldg}%' ORDER BY dateposted DESC LIMIT $start, $limit";
$result = mysql_query($query1);
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = '';
if($lastpage > 1)
{
$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1&search=$search&position=$position&dept=$dept&bldg=$bldg''>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1&search=$search&position=$position&dept=$dept&bldg=$bldg'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}
$paginate.= "</div>";
}
echo $total_pages.' Results';
// pagination
echo $paginate;
?>
<ul>
<?php
echo "<table id='t01' border='5' style='width:100%' align='center'>";
echo "<tr>";
echo "<th>NAME</th>";
echo "<th>CONTACT</th>";
echo "<th>DEPT</th>";
echo "<th>BLDG</th>";
echo "<th>POSITION</th>";
echo "<th>COMMENT</th>";
echo "</tr>";
while($row = mysql_fetch_array($result))
{
// echo '<li>'.$row['user'].'</li>';
echo "<tr>";
echo "<td>" . $row['user'] . "</td>";
echo "<td>" . $row['contact'] . "</td>";
echo "<td>" . $row['dept'] . "</td>";
echo "<td>" . $row['bldg'] . "</td>";
echo "<td>" . $row['position'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
?>
答案 0 :(得分:0)
当您创建分页链接时,您不会在创建的链接中添加查询参数,这是当您转到下一页时缺少参数的原因。
所以无论你在哪里创建像这样的分页链接
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
与pagenum一起添加搜索的参数。例如(添加搜索项目)
$paginate.= "<a href='$targetpage?page=$LastPagem1&search=$search'>$LastPagem1</a>";
除此之外,您似乎从$ _POST变量获取数据,现在当您在链接上添加参数时,您必须通过$ _GET获取它们 喜欢
$_GET['search']
等等......
假设第一次进行搜索,它是使用表单上的method="post"
发送的,如果适合您的用例,您可以将其更改为method="get"
。
更新:
您需要将查询参数添加到所有链接,而不仅仅是在一个地方。
所以你可以先做的是创建一个已经连接了所有查询参数的变量
$params = "&search=$search&position=$position&dept=$dept&bldg=$bldg";
然后,无论你在哪里编写类似
的代码 <a href='$targetpage?page=$LastPagem1'>
在那里添加params变量,如:
<a href='$targetpage?page=$LastPagem1{$params}'>
请记住,您需要在代码中传递?page=
的所有位置进行此操作,或者仅检查您更新的链接。