[英]How to release pagination in “GET” search?
我使用此代码在数据库中进行搜索:
<?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代码,
首先检查网址中是否存在...&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
则此子集范围将是$total_pages = 20
或6-10等,它可以是1到20之间的任何连续五页。此外,必要时请调整此范围。 此步骤的代码如下:
// 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.'">';
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.