gpt4 book ai didi

php mysql过滤的分页结果在下一页上不起作用

转载 作者:行者123 更新时间:2023-11-30 22:25:27 25 4
gpt4 key购买 nike

美好的一天。我需要你的专家意见。我一直在使用来自 mysql 的过滤结果进行分页。

过滤后的结果仅适用于第一页。单击下一页或后续页面,出现以下错误,它还显示数据库中的所有数据。

Notice: Undefined index: search Notice: Undefined index: position Notice: Undefined index: dept Notice: Undefined index: bldg and so on...

我想知道我可能会遗漏什么......

这里是更新后的代码:

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

与页码一起添加搜索的参数。例如(添加搜索项)

$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= 的所有地方完成或者为了测试只检查您更新的链接。

关于php mysql过滤的分页结果在下一页上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com