gpt4 book ai didi

php - 将搜索或过滤器应用于带分页的表

转载 作者:行者123 更新时间:2023-11-29 10:42:44 24 4
gpt4 key购买 nike

您好,先生们,您能帮我解决这个代码吗?我尝试做的是当我在搜索框中输入某些内容时,例如。待处理 它将每页显示 5 个待处理的预订(5 行待处理的预订)。但当我尝试时,它显示了所有超过 10 的待处理预订。

这是图片 enter image description here

我尝试了这样的事情..但它什么也没显示

$query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%' LIMIT " . $this_page_first_result . ',' . $results_per_page"; 

这是完整的代码

 <?php
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ERROR | E_PARSE);
session_start();

?>

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "srdatabase";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}

$results_per_page = 5;
$select= "SELECT * FROM reservations";
$result = mysqli_query($conn, $select);
$number_of_results = mysqli_num_rows($result);

if(!isset($_GET['page']))
{
$page = 1;
}
else
{
$page = $_GET['page'];
}

$this_page_first_result = ($page-1)*$results_per_page;

$sql = "SELECT * FROM reservations LIMIT " . $this_page_first_result . ',' . $results_per_page;
$result = mysqli_query($conn, $sql);

$number_of_pages = ceil($number_of_results/$results_per_page);


?>

<div id="paging-div">
<?php
for($page=1;$page<=$number_of_pages;$page++)
{
echo '<a id="pagingLink" href="adminControl.php?page=' . $page . '">' . $page . '</a>';
}
?>

<?php
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
$query = "SELECT * FROM reservations WHERE CONCAT(firstname, lastname, reservationstatus)LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else
{
$query = "SELECT * FROM reservations";
$search_result = filterTable($query);
}

function filterTable($query)
{
$conn = mysqli_connect("localhost", "root", "", "srdatabase");
$filter_Result = mysqli_query($conn, $query);
return $filter_Result;
}
?>


</div>
<!DOCTYPE html>
<html>
<head>
<title>Admin Control</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

<div class="topnav" id="myTopnav">
<a href="index.php">Home</a>
<a href="speaker.php">Speakers</a>
<a href="about.php">About</a>
<a href="contact.php">Contact</a>
<a href="reservation.php">Reservation</a>
<a href="signOut.php" id="signOut" style="float:right">Sign Out</a>
<a href="user.php" id="user" style="float:right; text-transform:capitalize;"><?php echo $_SESSION['firstname']; ?></a>
<a href="signUp.php" id="signUp" style="float:right">Sign Up</a>
<a href="signIn.php" id="signIn" style="float:right" onclick="document.getElementById('id01').style.display='block'">Sign In</a>
<a href="adminControl.php" id="adminControl" style="float:right; width:110px;">Admin control</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<br>
<br>
<br>
<br>
<h4 style="padding-left:10px; text-align:center;">Reservation List</h4>
<hr>

<form action="adminControl.php" method="POST">
<input type="text" name="valueToSearch" placeholder="type a value">
<input type="submit" name="search" value="Filter">
</form>
<br>
<br>
<div style="overflow-x:auto;">
<table class="reservations-table">
<tr>
<th class="thFirstName">First Name</th>
<th class="thLastName">Last Name</th>
<th class="thEmailAddress">Email Address</th>
<th class="thContactNumber">Contact Number</th>
<th class="thSpeaker">Speaker</th>
<th class="thTopic">Topic</th>
<th class="thLocation">Location</th>
<th class="thAudience">Audience</th>
<th class="thCount">Count</th>
<th class="thTime">Time</th>
<th class="thDate">Date</th>
<th class="thAction">Reservation Date</th>
<th class="thAction">Status</th>
<th class="thAction">Action</th>
<th class="thAction">Action</th>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<td><?php echo $row['firstname'];?></td>
<td><?php echo $row['lastname'];?></td>
<td><?php echo $row['emailaddress'];?></td>
<td><?php echo $row['contactnumber'];?></td>
<td><?php echo $row['speaker'];?></td>
<td><?php echo $row['topic'];?></td>
<td><?php echo $row['location'];?></td>
<td><?php echo $row['audience'];?></td>
<td><?php echo $row['count'];?></td>
<td><?php echo $row['time'];?></td>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['reservationdate'];?></td>
<td><?php echo $row['reservationstatus'];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</div>

<?php

$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];

if($epr=='delete')
{
$id=$_GET['id'];
$delete=mysqli_query($conn, "DELETE FROM reservations WHERE id=$id");
if($delete)
header('location:adminControl.php');
else
$msg='Error :'.mysqli_error();
}
?>

<?php

$epr='';
$msg='';
if(isset($_GET['epr']))
$epr=$_GET['epr'];

if($epr=='approve')
{
$id=$_GET['id'];
$approve=mysqli_query($conn, "UPDATE reservations SET reservationstatus='approved' WHERE id=$id");
header('location:adminControl.php');
}
?>

<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>

<script>
function ifAdmin()
{
document.getElementById("signIn").style.display = "none";
document.getElementById("signUp").style.display = "none";
document.getElementById("signOut").style.display = "block";
document.getElementById("adminControl").style.display = "block";
}
</script>

<script>
function ifNotAdmin()
{
document.getElementById("signIn").style.display = "none";
document.getElementById("signUp").style.display = "none";
document.getElementById("signOut").style.display = "block";
document.getElementById("adminControl").style.display = "none";
}
</script>

<script>
function ifNotLogin()
{
document.getElementById("user").style.display = "none";
document.getElementById("signOut").style.display = "none";
document.getElementById("adminControl").style.display = "none";
}
</script>

<?php

if (isset($_SESSION['signedIn']) && $_SESSION['signedIn'] == true)
//if login
{
if($_SESSION['type'] == 1)
{
echo "<script type='text/javascript'>ifAdmin();</script>";
}
elseif($_SESSION['type'] == 0)
{
echo "<script type='text/javascript'>ifNotAdmin();</script>";
}
}
//if not login
else
{
echo "<script type='text/javascript'>ifNotLogin();</script>";
}
?>

<div id="footer" class="push">Copyright 2017</div>

</body>
</html>

最佳答案

... when i try it, it shows all the pending reservation which is more than 10.

这是因为当您点击第二页、第三页……(从第一页导航后)时,$_POST 数组将为空,即 $_POST['search'] 不会被设置,这就是为什么每次导航到第 2、第 3、... 页面时都会执行 else{...} 部分代码。由于您不会通过表单发送任何敏感数据,因此请在表单的 method 属性中使用 GET 而不是 POST,如下所示:

<form action="..." method="get">

并获取用户输入的数据,如下所示:

if (isset($_GET['search'])) {
$valueToSearch = $_GET['valueToSearch'];
...

随后,您需要在每个分页链接中附加该搜索查询,以便在您从一个页面跳转到另一个页面时可以使用该搜索查询。

// your code
<?php
for($page=1;$page<=$number_of_pages;$page++)
{
echo "<a id='pagingLink' href='adminControl.php?page=" . $page . "&valueToSearch=". urlencode($_GET['valueToSearch']) ."&search'>" . $page . "</a>";
}
?>
// your code

关于php - 将搜索或过滤器应用于带分页的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176497/

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