我已经在php中为我的网站实施了分页系统,唯一的问题是如下:
假设我的网址分页为www.abc.com/search.php?mode=study&query=a
,目前每页显示20个结果,当我使用分页浏览结果时,网址会更改为www.abc.com/search.php?mode=study&query=a&page=2
,www.abc.com/search.php?mode=study&query=a&page=2&page=3
和{{ 1}},所以我面临的问题是当我浏览结果时,每次页面变量都附加到网址时,这真的很烦人。
我的分页代码是。
www.abc.com/search.php?mode=study&query=a&page=2&page=3&page=4
我知道问题在于<?php
function displayPaginationBelow($per_page,$page,$total){
$page_url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]&";
$adjacents = "2";
$page = ($page == 0 ? 1 : $page);
$start = ($page - 1) * $per_page;
$prev = $page - 1;
$next = $page + 1;
$setLastpage = ceil($total/$per_page);
$lpm1 = $setLastpage - 1;
$setPaginate = "";
if($setLastpage > 1)
{
$setPaginate .= "<ul class='setPaginate'>";
$setPaginate .= "<li class='setPage'>Page $page of $setLastpage</li>";
if ($setLastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $setLastpage; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
elseif (isset($_GET['page'])) {
unset($_GET['page']);
$page_url = http_build_query($_GET);
echo $page_url;
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
}
elseif($setLastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
$setPaginate.= "<li class='dot'>...</li>";
$setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";
}
elseif($setLastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
$setPaginate.= "<li class='dot'>...</li>";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
$setPaginate.= "<li class='dot'>..</li>";
$setPaginate.= "<li><a href='{$page_url}page=$lpm1'>$lpm1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>$setLastpage</a></li>";
}
else
{
$setPaginate.= "<li><a href='{$page_url}page=1'>1</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=2'>2</a></li>";
$setPaginate.= "<li class='dot'>..</li>";
for ($counter = $setLastpage - (2 + ($adjacents * 2)); $counter <= $setLastpage; $counter++)
{
if ($counter == $page)
$setPaginate.= "<li><a class='current_page'>$counter</a></li>";
else
$setPaginate.= "<li><a href='{$page_url}page=$counter'>$counter</a></li>";
}
}
}
if ($page < $counter - 1){
$setPaginate.= "<li><a href='{$page_url}page=$next'>Next</a></li>";
$setPaginate.= "<li><a href='{$page_url}page=$setLastpage'>Last</a></li>";
}else{
$setPaginate.= "<li><a class='current_page'>Next</a></li>";
$setPaginate.= "<li><a class='current_page'>Last</a></li>";
}
$setPaginate.= "</ul>\n";
}
return $setPaginate;
}
变量,需要帮助解决这个小问题
答案 0 :(得分:1)
$page_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //Create the url
$page_url = preg_replace("/(page\=[0-9]+[\&]*)/", "", $page_url); // Delete page query string.
if($page_url[strlen($page_url) - 1 ] !== "&"){ // If last char is not &
$page_url .= "&"; // add &
}
答案 1 :(得分:0)
在添加新页面参数之前,您需要删除旧页面参数。因此,请通过unset()
在this answer中显示了一种方法。