我搜索了类似的问题,并在这里,只有答案,发现一个,但没有得到证实的问题是如何固定的(will_paginate - other queries not showing on page 2 and beyond)PHP PAGINATE 2
基本上我使用过的PAGINATE一个去同一个测试页面一个搜索框,然后在每个页面上只回显一个结果。它的作品在第一页上,但在第二页上没有显示任何内容。
任何人都可以建议,如果我失去了一些愚蠢的东西或缺少点?在我的页面上它只是一个文本框和搜索,然后我希望它分页如果不止一个结果。
继承人的完整代码
<?php
\t require ("cw/connect.php");
if(isset($_POST['search'])) {
$search = mysqli_real_escape_string($connect,$_POST['search']);
$search = preg_replace("#[^0-9a-z]i#","", $search);
\t $start=0;
\t $limit=2;
//Check Form is not blank
\t if (empty($search)){
$msg = "Please enter a search criteria";
\t }
\t else {
\t \t
$query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE
\t first LIKE '%".$search."%'
\t OR
\t last LIKE '%".$search."%'
\t OR
\t email LIKE '%".$search."%'
\t OR
\t about LIKE '%".$search."%'
\t OR
\t username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");
\t $count = mysqli_num_rows($query);
\t
\t if(isset($_GET['id']))
\t {
\t \t $id=$_GET['id'];
\t \t $start=($id-1)*$limit;
\t }
\t else
\t {
\t \t $id=1;
\t }
\t $page=ceil($count/$limit);
$query = mysqli_query($connect,"SELECT id, username, first, last, email, about FROM users WHERE
\t first LIKE '%".$search."%'
\t OR
\t last LIKE '%".$search."%'
\t OR
\t email LIKE '%".$search."%'
\t OR
\t about LIKE '%".$search."%'
\t OR
\t username LIKE '%".$search."%' LIMIT $start, $limit") or die ("There was no search results!");
\t \t
\t \t
if($count == 0){
$msg = "There was no search results!";
}else{
while ($row = mysqli_fetch_array($query)) {
\t \t
$id = $row ['id'];
$username = $row ['username'];
$first = $row ['first'];
$last = $row ['last'];
$email = $row ['email'];
$about = $row ['about'];
\t \t
\t \t //The layout and what will be shown on page
$msg .='<p><b>ID:</b> '.$id.
\t \t '<br><b>User:</b> '.$username.
\t \t '<br><b>First Name:</b> '.$first.
\t \t '<br><b>Last Name:</b> '.$last.
\t \t '<br><b>Email:</b> '.$email.
\t \t '<br><b>About:</b> '.$about.
\t \t '</p>
\t \t <hr>';
}
\t ?>
\t
\t <ul>
\t
\t <?php
\t if($id > 1) {
\t ?>
\t
\t <li><a href="?id=<?php echo ($id-1);?>">Previous</a></li>
\t <?php } ?>
\t
\t
\t <?php
\t for($i=1;$i <= $page; $i++)
\t {
\t ?>
\t
\t \t <li><a href="?id=<?php echo $i;?>"><?php echo $i; ?></a></li>
\t <?php \t \t
\t }
\t ?>
\t
\t <?php
\t if($id!=$page)
\t {
\t \t
\t ?>
\t
\t <li><a href="?id=<?php echo ($id+1);?>">Next</a></li>
\t
\t </ul>
\t
\t <?php
}
}
}
\t }
?>
<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/>
你为什么要包括'CW/connect.php'两次? –
抱歉,我复印时出现错误,现在我已将其删除 – CwStrange
为什么您首先获取所有匹配的记录,然后获取具有限制和偏移量的_all_记录(无条件)?您还应该检查第二个查询的实际外观。 –