如何将搜索引擎添加到我的分页中
How do I add search engine into my pagination
我的分页使用了 table,但我想要一个搜索引擎,如何将搜索引擎添加到其中?我在里面有一个简单的搜索表单,但不知道如何集成进去。
<html>
<head>
<title>Search</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<form method="get" action="index.php">
<label>Search: <input type="text" name="query"/>
<input type="submit" name = "search" value="search" />
</form>
<?php
include('db.php');
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$total_records_per_page = 10;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `cases`");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
$result = mysqli_query($con,"SELECT * FROM `cases` LIMIT $offset, $total_records_per_page");
while($row = mysqli_fetch_array($result)){
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['email']."</td>
<td>".$row['phone']."</td>
<td>".$row['case']."</td>
</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
您可以将搜索项添加到 sql 查询中:
<form method="get" action="index.php">
<!-- Show query item after page reloading -->
<label>Search: <input type="text" name="query" value="<?php echo htmlentities(isset($_GET['query']) ? $_GET['query'] : ''); ?>"/>
<input type="submit" name = "search" value="search" />
</form>
<?php
include('db.php');
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$where = '';
if (!empty($_GET['query'])) {
//it is important to escape search item before query
$query = mysqli_real_escape_string($con, $_GET['query']);
//there are can be any conditions
//lets say you want to find user by name
$where = "WHERE cases.name = '{$query}'";
}
//items for pager need be calculated with query too
$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `cases` {$where}");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
//add condition to data query
$result = mysqli_query($con,"SELECT * FROM `cases` {$where} LIMIT $offset, $total_records_per_page");
我推荐你使用PDO with prepared statements因为它们更紧凑也更安全。
我的分页使用了 table,但我想要一个搜索引擎,如何将搜索引擎添加到其中?我在里面有一个简单的搜索表单,但不知道如何集成进去。
<html>
<head>
<title>Search</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<form method="get" action="index.php">
<label>Search: <input type="text" name="query"/>
<input type="submit" name = "search" value="search" />
</form>
<?php
include('db.php');
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$total_records_per_page = 10;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2";
$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `cases`");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
$result = mysqli_query($con,"SELECT * FROM `cases` LIMIT $offset, $total_records_per_page");
while($row = mysqli_fetch_array($result)){
echo "<tr>
<td>".$row['name']."</td>
<td>".$row['email']."</td>
<td>".$row['phone']."</td>
<td>".$row['case']."</td>
</tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
您可以将搜索项添加到 sql 查询中:
<form method="get" action="index.php">
<!-- Show query item after page reloading -->
<label>Search: <input type="text" name="query" value="<?php echo htmlentities(isset($_GET['query']) ? $_GET['query'] : ''); ?>"/>
<input type="submit" name = "search" value="search" />
</form>
<?php
include('db.php');
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
$page_no = 1;
}
$where = '';
if (!empty($_GET['query'])) {
//it is important to escape search item before query
$query = mysqli_real_escape_string($con, $_GET['query']);
//there are can be any conditions
//lets say you want to find user by name
$where = "WHERE cases.name = '{$query}'";
}
//items for pager need be calculated with query too
$result_count = mysqli_query($con,"SELECT COUNT(*) As total_records FROM `cases` {$where}");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1
//add condition to data query
$result = mysqli_query($con,"SELECT * FROM `cases` {$where} LIMIT $offset, $total_records_per_page");
我推荐你使用PDO with prepared statements因为它们更紧凑也更安全。