我寫了這個代碼表中的行搜索:搜索PHP/MySQL與分頁
<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頁。
任何人都可以幫助我解決這些問題?