美好的一天。我需要您的专家意见。我一直在尝试从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>";
}
?>
最佳答案
创建分页链接时,没有在创建的链接中添加查询参数,这是当您转到下一页时缺少参数的原因。
所以无论您在哪里创建这样的分页链接$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=
的所有位置进行,或者仅在测试时检查更新的链接。