我使用这个代码在数据库中搜索:
<?php
if(isset($_GET['keywords'])) {
$keywords = $CONNECT->escape_string($_GET['keywords']);
if(!$keywords){
echo("<script>location.href = '/news';</script>");
//if javascript disabled
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
}
$Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news` WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'");
?>
<div class="found">founded<?php echo $Query->num_rows; ?> </div>
</div>
</div>
<div class="ncontent">
<div class="keyword">phrase: "<p><?php echo htmlspecialchars($keywords); ?></p>"</div>
<?php
if($Query->num_rows){
while ($r = $Query->fetch_object()){
?>
<div class="nline"><a href="/news/content/id/<?php echo $r->id; ?>"><div class="prewnews">
<div class="imgh1"><img src="<?php echo $r->himg; ?>"/></div>
<span><i class="fa fa-heart-o" aria-hidden="true"> <p>124</p></i><hr style="background:#cd4436; border:0; height:3px" /><i class="fa fa-eye" aria-hidden="true"> <p><?php echo $r->readed; ?></p></i> </span>
<h3><?php echo $r->title; ?></h3>
</div>
</a>
</div>
<?php
}
}else echo '<div class="notfound"><img src="/resource/img/notfound.png" alt=""></div>';
}else {echo("<script>location.href = '/news';</script>");
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';}
?>
搜索表单:
<form action="/search" method="GET">
<input class="search red" placeholder="search..." type="search" name="keywords" autocomplete="off">
</form>
所以,搜索后我得到这样的链接:/search?keywords=test+search+phrase
我想我可以这样做:/search?keywords=test+search+phrase&page2
或者像这样:/page2/search?keywords=test+search+phrase
你建议用哪种方式分页?
你能提供帮助实现这些目标的链接吗?
或者你们有人能帮我吗?
下面的分页解决方案将在每个页面最多显示10个搜索结果,对于导航其余的搜索结果,使用分页链接。解决方案是每页最多显示5个链接,就像这样:
// user is on 1st page
1 2 3 4 5 ...
-
// user is on 7th page
... 5 6 7 8 9 ...
-
// user is on 10th page(lets say, 10th page is the last page)
... 6 7 8 9 10
-
因此,要实现这个分页功能,您需要在代码中做一些更改。保持HTML搜索表单不变,并按以下方式更改PHP代码
首先检查URL中是否存在
...&page=123
,并基于此,像这样处理$_GET
超全局变量,if(isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page'])){ $current_page = $_GET['page']; }else{ $current_page = 1; }
定义每页行数并以此为基础计算偏移量,如下所示:
$per_page = 10; // rows per page $offset = ($current_page - 1) * $per_page; // offset
根据
$per_page
和$offset
的值获取表结果,如下所示:$Query = mysqli_query($CONNECT, "SELECT ... WHERE `content` LIKE ... LIMIT ". $per_page . " OFFSET " . $offset);
现在要在搜索结果下面显示分页链接,请遵循以下步骤:
从查询的结果集中获取总行数,并计算要显示的页面/分页链接的总数,如下所示:
// display pagination links $Query = mysqli_query($CONNECT, "SELECT ... FROM `news` WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'"); if($Query->num_rows){ $total_records = $Query->num_rows; $total_pages = ceil($total_records / $per_page); ...
查找页面的超集范围,如1-10或1-20等。例如,如果
$total_pages = 20
,那么这个超集范围将是1-20。这一步的代码如下:// superset range of pages $superset_range = range(1, $total_pages);
查找要显示的页面子集范围,如1-5或3-7等。例如,如果
$total_pages = 20
,那么这个子集范围将是1-5,或3-7,或6-10等,它可以是1到20之间的任何连续的5页。此外,在必要时调整这个范围。这一步的代码如下:// subset range of pages to display $subset_range = range($current_page - 2, $current_page + 2); // adjust the range(if required) foreach($subset_range as $p){ if($p < 1){ array_shift($subset_range); if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){ $subset_range[] = $subset_range[count($subset_range) - 1] + 1; } }elseif($p > $total_pages){ array_pop($subset_range); if(in_array($subset_range[0] - 1, $superset_range)){ array_unshift($subset_range, $subset_range[0] - 1); } } }
最后,显示相应的分页链接和点。这一步的代码如下:
// display intermediate pagination links if($subset_range[0] > $superset_range[0]){ echo "... "; } foreach($subset_range as $p){ if($p == $current_page){ echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "' style='text-decoration: underline;'>$p</a>"; }else{ echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "'>$p</a>"; } } if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){ echo " ... "; }
下面是完整的代码:
<?php
if(isset($_GET['keywords'])) {
$keywords = $CONNECT->escape_string($_GET['keywords']);
if(!$keywords){
echo("<script>location.href = '/news';</script>");
//if javascript disabled
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
}
if(isset($_GET['page']) && !empty($_GET['page']) && is_numeric($_GET['page'])){
$current_page = $_GET['page'];
}else{
$current_page = 1;
}
$per_page = 10;
$offset = ($current_page - 1) * $per_page;
$Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news` WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%' LIMIT ". $per_page . " OFFSET " . $offset);
?>
<div class="found">founded<?php echo $Query->num_rows; ?> </div>
</div>
</div>
<div class="ncontent">
<div class="keyword">phrase: "<p><?php echo htmlspecialchars($keywords); ?></p>"</div>
<?php
if($Query->num_rows){
while ($r = $Query->fetch_object()){
?>
<div class="nline">
<a href="/news/content/id/<?php echo $r->id; ?>">
<div class="prewnews">
<div class="imgh1">
<img src="<?php echo $r->himg; ?>"/>
</div>
<span>
<i class="fa fa-heart-o" aria-hidden="true">
<p>124</p>
</i>
<hr style="background:#cd4436; border:0; height:3px" />
<i class="fa fa-eye" aria-hidden="true">
<p><?php echo $r->readed; ?></p>
</i>
</span>
<h3><?php echo $r->title; ?></h3>
</div>
</a>
</div>
<?php
}
// display pagination links
$Query = mysqli_query($CONNECT, "SELECT `id`, `title`, `himg`, `readed` FROM `news` WHERE `content` LIKE '%{$keywords}%' OR `title` LIKE '%{$keywords}%'");
if($Query->num_rows){
$total_records = $Query->num_rows;
$total_pages = ceil($total_records / $per_page);
if($total_records > $per_page){
echo "<center>";
// Superset range of pages
$superset_range = range(1, $total_pages);
// subset range of pages to display
$subset_range = range($current_page - 2, $current_page + 2);
// adjust the range(if required)
foreach($subset_range as $p){
if($p < 1){
array_shift($subset_range);
if(in_array($subset_range[count($subset_range) - 1] + 1, $superset_range)){
$subset_range[] = $subset_range[count($subset_range) - 1] + 1;
}
}elseif($p > $total_pages){
array_pop($subset_range);
if(in_array($subset_range[0] - 1, $superset_range)){
array_unshift($subset_range, $subset_range[0] - 1);
}
}
}
// display intermediate pagination links
if($subset_range[0] > $superset_range[0]){
echo "... ";
}
foreach($subset_range as $p){
if($p == $current_page){
echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "' style='text-decoration: underline;'>$p</a>";
}else{
echo "<a href='/search?keywords=" . $keywords . "&page=" . $p . "'>$p</a>";
}
}
if($subset_range[count($subset_range) - 1] < $superset_range[count($superset_range) - 1]){
echo " ... ";
}
echo "</center> ";
}
}
}else{
echo '<div class="notfound"><img src="/resource/img/notfound.png" alt=""></div>';
}
}else {
echo("<script>location.href = '/news';</script>");
$url='/news';
echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';
}
?>