我写了这段代码来搜索表格的行:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$page_max = 8;
if (isset($_POST['search'])){
$search = $_POST['searchValue'];
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers WHERE name LIKE '%$search%'");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers WHERE name LIKE '%$search%' LIMIT $start, $numberOfRecords");
}
else{
$entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
$numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
$numberOfRecords = $page_max;
if(isset($_GET['page'])) {
$page = $_GET['page'];
$start = $page * $page_max;
}
else{
$page = 0;
$start = $page * $page_max;
}
$customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
if(isset($_GET['page']) && $_GET['page'] < $numberOfPages - 1){
$page = $_GET['page'];
$next = $page + 1;
echo '<li class="page-item"><a class="page-link" href="?page='.$next.'">Next</a></li> ';
}
elseif(!isset($_GET['page'])){
echo '<li class="page-item"><a class="page-link" href="?page=1">Next</a></li> ';
}
?>
</ul>
</div>
当我在第0页时,搜索工作正常,但是当我在第2页时,我只能搜索该页面上的记录(我认为这是因为我的查询中的限制)除了我搜索时'A'它返回7页,但如果我点击seconde页面,它将不会加载搜索结果,而是加载第2页的客户主列表。
有人可以帮我解决这些问题吗?
答案 0 :(得分:0)
首先:清理您的输入。在数据库查询中使用POST或GET可能非常非常危险。
第二:DRY,您可能只需要使用一半代码即可。
您“失去”搜索的原因是因为您的分页不会延续搜索字词。
将其添加到分页中的链接并从POST切换到GET:
<?php
$searchString = '';
if($_GET['searchValue']){
$searchString = '&searchValue=' . $_GET['searchValue'];
}
if(isset($_GET['page']) && $_GET['page'] > 0){
$previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.$searchString.'">Previous</a></li>';
}
// more code
'... href="?page='.$i.$searchString'" ...'
'... href="?page='.$next.$searchString'" ...'
请注意,我使用$_GET['searchValue']
而不是POST。在大多数情况下,使用POST进行搜索没有任何优势(恰恰相反,如果您使用GET,则可以在Google Analytics f.ex中进行跟踪)。因此,请务必将表单方法更改为if (isset($_POST['search'])){
到$_GET['searchValue']
。如果您坚持使用POST,则可以使用$_REQUEST
,其中包含来自GET和POST的值。