分页链接中重复参数的问题? [英] A problem of a repeated parameter in the pagination links?

查看:102
本文介绍了分页链接中重复参数的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,例如,当我加载第2页时,URL变为:

The problem is that when I load page 2 for example the URL becomes:

http://domain.com/index.php?restaurant -id = 45& currentpage = 2

那很好,但是当我进入第3页时,它变成:

And that's fine but when I get to page 3 it becomes:

http://domain.com/index .php?restaurant-id = 45& currentpage = 2& currentpage = 3

依次类推,每次从分页链接加载新页面时,它都会添加一个currentpage参数!

And so on, it adds one more currentpage parameter everytime a new page is loaded from the pagination links!

我想知道如何解决此问题?

I wonder how this problem can be fixed?

以下是分页功能的一些代码

Here's some of the pagination function's code

 /******  build the pagination links ******/
// Getting current page URL with its parameters
$current_page_url = ($_SERVER["PHP_SELF"].(isset($_SERVER["QUERY_STRING"])?"?".htmlentities($_SERVER["QUERY_STRING"]):""));
// Determine which sign to use (? or &) before the (currentpage=xx) parameter
$sign = preg_match('/\?/', $current_page_url) ? '&' : '?';

$pagination_links = '';
// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   $pagination_links .= " <a href='{$current_page_url}{$sign}currentpage=1'>First page</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back 1 page
   $pagination_links .= " <a href='{$current_page_url}{$sign}currentpage=$prevpage'>previous</a> ";
}
else
{
    $pagination_links .= "ـ ـ"; 
}// end if 

推荐答案

尽管丑陋,但您可以使用正则表达式从$current_page_url变量中删除当前页参数.

You could remove currentpage parameter from $current_page_url variable with regex although it's ugly.

$current_page_url = preg_replace('currentpage=\d+', '', $current_page_url);

但是,我建议编写一个小的函数,该函数将在传递给它的值上生成URL,然后您可以轻松地修改任何参数,而不必担心正则表达式并将其从URL中删除. 应该很好,例如.

However I'd suggest writing a small function that will generate URLs on the values you pass to it and then you can easily modify any parameter without having to worry about regexes and removing them from the URL. Something like this should be fine.

这篇关于分页链接中重复参数的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆