使用php創建一個新項目:使用搜索功能從mysql數據庫中檢索數據(search.php) - 爲搜索結果頁面(find.php)添加分頁。問題是,當顯示第一個搜索時,它顯示正確的數據;然而,當我點擊第二頁(分頁按鈕)時,它再次填充所有數據 - 不是我搜索的術語:請幫助!MySQL PHP搜索結果與分頁
的search.php
<form action="find.php" method="post">
<input name="search" type="search" autofocus><input type="submit" name="button">
</form>
find.php
<?php
include_once("config.php");
if(isset($_POST['button'])){
$search=$_POST['search'];
}
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$row = mysql_num_rows($result);
$page_rows = 3;
$last = ceil($row/$page_rows);
if($last < 1){
$last = 1;
}
$pagenum = 1;
if(isset($_GET['page'])){
$pagenum = preg_replace('#[^0-9]#', '', $_GET['page']);
}
if ($pagenum < 1) {
$pagenum = 1;
} else if ($pagenum > $last) {
$pagenum = $last;
}
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result = mysql_query(" SELECT * FROM bra_list WHERE locations like '%{$search}%' $limit ");
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";
$paginationCtrls = '';
$self = $_SERVER['PHP_SELF'];
if($last != 1){
if ($pagenum > 1) {
$previous = $pagenum - 1;
$paginationCtrls .= '<a href="'.$self.'?page='.$previous.'">Previous</a>';
for($i = $pagenum-4; $i < $pagenum; $i++){
if($i > 0){
$paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>';
}
}
}
$paginationCtrls .= '<span class="current">'.$pagenum.'</span>';
for($i = $pagenum+1; $i <= $last; $i++){
$paginationCtrls .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a>';
if($i >= $pagenum+4){
break;
}
}
if ($pagenum != $last) {
$next = $pagenum + 1;
$paginationCtrls .= '<a href="'.$self.'?page='.$next.'">Next</a> ';
}
}
?>
<table border='1' cellpadding='10' style="border-collapse:collapse;">
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>website</th>
<th>locations</th>
<th>info</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['website']; ?></td>
<td><?php echo $row['locations']; ?></td>
<td><?php echo $row['info']; ?></td>
</tr>
<?php
}
?>
第一個查詢不應該包含限制。第二個應該包含一個限制和一個抵消 –