我在这里搜索了类似的问题和答案,但只找到一个,但未确认问题是如何解决的(will_paginate - other queries not showing on page 2 and beyond)
基本上我在使用搜索框的测试页面进行分页,然后在每个页面上只回显一个结果。它在第一页上工作但在第2页上没有显示任何内容。
任何人都可以告诉我,如果我错过了一些愚蠢的事情或者错过了这一点吗?在我的页面上它只是一个文本框和搜索,然后我希望它分页如果有多个结果。
下载完整的代码
<?php
require ("cw/connect.php");
if(isset($_POST['search'])) {
$search = mysqli_real_escape_string($connect,$_POST['search']);
$search = preg_replace("#[^0-9a-z]i#","", $search);
$start=0;
$limit=2;
//Check Form is not blank
if (empty($search)){
$msg = "Please enter a search criteria";
}
else {
$query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE
first LIKE '%".$search."%'
OR
last LIKE '%".$search."%'
OR
email LIKE '%".$search."%'
OR
about LIKE '%".$search."%'
OR
username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");
$count = mysqli_num_rows($query);
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
else
{
$id=1;
}
$page=ceil($count/$limit);
$query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE
first LIKE '%".$search."%'
OR
last LIKE '%".$search."%'
OR
email LIKE '%".$search."%'
OR
about LIKE '%".$search."%'
OR
username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");
if($count == 0){
$msg = "There was no search results!";
}else{
while ($row = mysqli_fetch_array($query)) {
$id = $row ['id'];
$username = $row ['username'];
$first = $row ['first'];
$last = $row ['last'];
$email = $row ['email'];
$about = $row ['about'];
//The layout and what will be shown on page
$msg .='<p><b>ID:</b> '.$id.
'<br><b>User:</b> '.$username.
'<br><b>First Name:</b> '.$first.
'<br><b>Last Name:</b> '.$last.
'<br><b>Email:</b> '.$email.
'<br><b>About:</b> '.$about.
'</p>
<hr>';
}
?>
<ul>
<?php
if($id > 1) {
?>
<li><a href="?id=<?php echo ($id-1);?>">Previous</a></li>
<?php } ?>
<?php
for($i=1;$i <= $page; $i++)
{
?>
<li><a href="?id=<?php echo $i;?>"><?php echo $i; ?></a></li>
<?php
}
?>
<?php
if($id!=$page)
{
?>
<li><a href="?id=<?php echo ($id+1);?>">Next</a></li>
</ul>
<?php
}
}
}
}
?>
<form action ="" method = "post">
<input name="search" type="text" size="30" placeholder="eg name"/>
<input type="submit" value="search"/> <?php echo "$msg";?>
</form>
</p>
<br/><br/>
&#13;
答案 0 :(得分:0)
我已经重新调整了代码。
所以我的结果是 - 小心 - 我没有测试过它:
require("cw/connect.php");
$msg = "";
if (isset($_GET['search'])) {
$search = mysqli_real_escape_string($connect,$_GET['search']);
$search = preg_replace("#[^0-9a-z]i#","", $search);
if (empty($search)) {
$msg = "Please enter a search criteria";
} else {
$items_per_page = 1;
$page_id = 1;
$start = 0;
// added a little validation
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 1) {
$page_id = $_GET['id'];
$start = ceil($page_id * $items_per_page);
}
$query = "SELECT id, username, first, last, email, about FROM users WHERE
first LIKE '%".$search."%' OR
last LIKE '%".$search."%' OR
email LIKE '%".$search."%' OR
about LIKE '%".$search."%' OR
username LIKE '%".$search."%'";
$result_query = mysqli_query($connect, $query . "LIMIT ".$start.",".$items_per_page) or die ("There are no search results!");
$count_query = mysqli_query($connect, $query) or die("There are no search results");
$count = mysqli_num_rows($count_query);
$page = ceil($count / $items_per_page);
if ($count <= 0) {
$msg = "There where no search results!";
} else {
while ($row = mysqli_fetch_array($result_query)) {
$user_id = $row ['id'];
$username = $row ['username'];
$first = $row ['first'];
$last = $row ['last'];
$email = $row ['email'];
$about = $row ['about'];
//The layout and what will be shown on page
$msg .= '<p><b>ID:</b> '.$user_id.
'<br><b>User:</b> '.$username.
'<br><b>First Name:</b> '.$first.
'<br><b>Last Name:</b> '.$last.
'<br><b>Email:</b> '.$email.
'<br><b>About:</b> '.$about.
'</p><hr>';
} ?>
<ul>
<?php if ($page_id > 1): ?>
<li><a href="/?id=<?php echo($page_id - 1); ?>&search=<?php echo $search; ?>">Previous</a></li>
<?php endif; ?>
<?php for ($i = 1; $i <= $page; $i++): ?>
<li><a href="/?id=<?php echo $i; ?>&search=<?php echo $search; ?>"><?php echo $i; ?></a></li>
<?php endfor; ?>
<?php if ($page_id != $page): ?>
<li><a href="/?id=<?php echo($page_id + 1); ?>&search=<?php echo $search; ?>">Next</a></li>
<?php endif; ?>
</ul>
<?php }
}
}
?>
<form action="" method="get">
<input name="search" type="text" size="30" placeholder="eg name"/>
<input type="submit" value="search"/>
</form>
<?php echo "$msg"; ?>