这是代码search.php - 此代码组合了来自数据库的搜索数据和分页无法正常工作。
<?php include_once('include/header.php'); ?>
<?php
//Declaration
$search_term = '';
$search_results = false;
require 'include/db.php';
//Check if search data was submitted
if (isset($_GET['pages'])) {
// Include the search class
require_once( dirname(__FILE__) . '/class-search.php' );
// Instantiate a new instance of the search class
$search = new search();
// Store search term into a variable
$search_term = $_GET['pages'];
// Send the search term to our search class and store the result
$search_results = $search->search($search_term);
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
</head>
<body>
<h1>Search</h1>
<div class="search-form">
<form action="" method="get">
<div class="form-field">
<label for="search-field">Search</label>
<input type="search" name="pages" placeholder="Enter your search term..." results="5" autocomplete="off">
<input type="submit" value="Search">
</div>
</form>
<?php
$perpage = 2;
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 1;
}
$start = ($page - 1) * $perpage;
?>
</div>
<?php
if ($search_results) :
?>
<div class="results-count">
<p>
<?php
echo $search_results['count'];
?> results found
</p>
</div>
<div class="results-table">
<table class="table table-hover">
<thead>
<tr>
<th>Number</th>
<th>Gender</th>
<th>Race</th>
<th>IC Number</th>
<th>Name</th>
<th>Address</th>
<th>Contact Number (Home)</th>
<th>Handphone Number</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<?php
$no = 1;
foreach ($search_results['results'] as $search_result) :
?>
<div class="result">
<tr>
<td><?php echo $no ?></td>
<td> <?php echo $search_result->cust_gender; ?> </td>
<td> <?php echo $search_result->cust_race; ?> </td>
<td> <?php echo $search_result->cust_ic; ?>
<td> <?php echo $search_result->cust_name; ?>
<td> <?php echo $search_result->cust_add1; ?>
<?php echo $search_result->cust_add2; ?>
<?php echo $search_result->cust_add3; ?>
<?php echo $search_result->cust_postcode; ?>
<?php echo $search_result->cust_town; ?>
<?php echo $search_result->cust_state; ?> </td>
<td><?php echo $search_result->cust_home_con; ?></td>
<td> <?php echo $search_result->cust_hp_contact1; ?>
<?php echo $search_result->cust_hp_contact2; ?> </td>
<td> <?php echo $search_result->cust_email; ?> </td>
</div>
<?php
$no++;
endforeach;
?>
</table>
</div>
<!--
<div class="search-raw">
<pre>
<?php
print_r($search_results);
?>
</pre>
</div>
-->
<?php
endif;
?>
<?php
$sql2 = "select * from customer_info ";
$query2 = mysqli_query($conn, $sql2);
$total_record = mysqli_num_rows($query2);
$total_page = ceil($total_record / $perpage);
?>
<nav>
<ul class="pagination">
<li>
<a href="search.php?pages=1" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<?php for ($i = 1; $i <= $total_page; $i++) { ?>
<li><a href="search.php?pages=<?php echo $i; ?>"><?php echo $i; ?></a></li>
<?php } ?>
<li>
<a href="search.php?pages=<?php echo $total_page; ?>" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</body>
</html>
这是代码类-search.php
$start = null;
$perpage = null;
class search {
private $mysqli;
public function __construct() {
// Connect to our database and store in $mysqli property
$this->connect();
}
private function connect() {
$this->mysqli = new mysqli( 'localhost', 'root', 'root', 'fimos' );
}
public function search($search_term) {
// Sanitize the search term to prevent injection attacks
$sanitized = $this->mysqli->real_escape_string($search_term);
// Run the query
$query = $this->mysqli->query("
SELECT *
FROM customer_info
WHERE cust_name LIKE '%{$sanitized}%'
OR cust_ic LIKE '%{$sanitized}%' {$start} , {$perpage}
");
// Check results
if ( ! $query->num_rows ) {
return false;
}
// Loop and fetch objects
while( $row = $query->fetch_object() ) {
$rows[] = $row;
}
// Build our return result
$search_results = array(
'count' => $query->num_rows,
'results' => $rows,
);
return $search_results;
}
}
为什么我仍然无法显示2个数据/页面?我点击分页功能1,2,3也同样记录,对我来说搜索页面1也改为结果只有1。我使用类进行搜索功能。希望可以帮助我。
答案 0 :(得分:0)
试试这个
before