删除 URL 中的旧参数 (PHP)

发布于 2024-09-19 12:27:59 字数 324 浏览 9 评论 0原文

我正在使用 PHP 为表格创建分页。 我使用以下代码创建分页链接,

<a class='page-numbers' href='$href&pagenum=$i'>$i</a>

使用 $href

$href = $_SERVER['REQUEST_URI'];

它工作得很好,但是,它与地址栏混淆,每次都会添加新的 pagenum 参数。 所以就变成了 pagenum=1&pagenum=3&pagenum=4....

如何改进呢?

I'm using PHP to create a pagination for a table.
I'm using the following code to create the pagination link

<a class='page-numbers' href='$href&pagenum=$i'>$i</a>

With $href

$href = $_SERVER['REQUEST_URI'];

It works well, however, it messes with the address bar, adding each time a new pagenum parameter.
So it becomes pagenum=1&pagenum=3&pagenum=4....

How to improve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在梵高的星空下 2024-09-26 12:27:59

这个怎么样?经过测试,可以肯定:)

<?php
    $new_get = $_GET; // clone the GET array
    $new_get['pagenum'] = $i; // change the relevant parameter
    $new_get_string = http_build_query($new_get); // create the foo=bar&bar=baz string
?>
<a class="page-numbers" href="?<?php echo $new_get_string; ?>">
    <?php echo $i ?>
</a>

另外,请注意整个 $href 位是不必要的。如果您以 ? 开头 href,浏览器会将查询字符串应用于当前路径。

不过,我敢打赌您会循环,因此这里有一个针对生成 10,000 个页码链接进行优化的版本。我的基准测试显示,在处理大量链接时,它的速度稍快一些,因为您只是进行字符串连接,而不是完整的 HTTP 查询构建,但这可能还不足以值得担心。仅当存在五六个 GET 参数时,差异才真正显着,但是,当存在时,此策略在我的计算机上只需大约一半的时间即可完成。

<?php
    $pageless_get = $_GET; // clone the GET array
    unset($pageless_get['pagenum']); // remove the pagenum parameter
    $pageless_get_string = http_build_query($pageless_get); // create the foo=bar&bar=baz string
    for($i = 0; $i < 10000; $i++):
        // append the pagenum param to the query string
        $page_param = "pagenum=$i";
        if($pageless_get_string) {
            $pageful_get_string = "$pageless_get_string&$page_param";
        } else {
            $pageful_get_string = $page_param;
        }
?>
    <a class="page-numbers" href="?<?php echo $pageful_get_string; ?>">
        <?php echo $i ?>
    </a>
<?php endfor ?>

How about this? Went and tested, to be sure :)

<?php
    $new_get = $_GET; // clone the GET array
    $new_get['pagenum'] = $i; // change the relevant parameter
    $new_get_string = http_build_query($new_get); // create the foo=bar&bar=baz string
?>
<a class="page-numbers" href="?<?php echo $new_get_string; ?>">
    <?php echo $i ?>
</a>

Also, note that the whole $href bit is unnecessary. If you start your href with ?, the browser will apply the query string to the current path.

I bet you're going to be looping, though, so here's a version optimized for producing 10,000 page number links. My benchmarks put it as being ever so slightly faster at large numbers of links, since you're just doing string concatenation instead of a full HTTP query build, but it might not be enough to be worth worrying about. The difference is only really significant when there five or six GET parameters, but, when there are, this strategy completes in about half the time on my machine.

<?php
    $pageless_get = $_GET; // clone the GET array
    unset($pageless_get['pagenum']); // remove the pagenum parameter
    $pageless_get_string = http_build_query($pageless_get); // create the foo=bar&bar=baz string
    for($i = 0; $i < 10000; $i++):
        // append the pagenum param to the query string
        $page_param = "pagenum=$i";
        if($pageless_get_string) {
            $pageful_get_string = "$pageless_get_string&$page_param";
        } else {
            $pageful_get_string = $page_param;
        }
?>
    <a class="page-numbers" href="?<?php echo $pageful_get_string; ?>">
        <?php echo $i ?>
    </a>
<?php endfor ?>
剩一世无双 2024-09-26 12:27:59
$url = $_SERVER['REQUEST_URI'];
$urlparams = parse_url($url);
if(isset($urlparams['query']){
   parse_str($urlparams['query'],$vars);
   $vars['pagenum'] = $i;
   $urlparams['query'] = http_build_query($vars);
} else {
   $urlparams['query'] = 'pagenum='.$i;
}
$url = http_build_url($urlparams);
//http_build_url() is in PECL, you might need to manually rebuild the 
//url by looping through it's components:
/*
   $url=(isset($urlparams["scheme"])?$urlparams["scheme"]."://":"").
       (isset($urlparams["user"])?$urlparams["user"]:"").
       (isset($urlparams["pass"])? ":".$urlparams["pass"]:"").
       (isset($urlparams["user"])?"@":"").
       (isset($urlparams["host"])?$urlparams["host"]:"").
       (isset($urlparams["port"])?":".$urlparams["port"]:"").
       (isset($urlparams["path"])?$urlparams["path"]:"").
       (isset($urlparams["query"])?"?".$urlparams["query"]:"").
       (isset($urlparams["fragment"])?"#".$urlparams["fragment"]:""); 
*/
$url = $_SERVER['REQUEST_URI'];
$urlparams = parse_url($url);
if(isset($urlparams['query']){
   parse_str($urlparams['query'],$vars);
   $vars['pagenum'] = $i;
   $urlparams['query'] = http_build_query($vars);
} else {
   $urlparams['query'] = 'pagenum='.$i;
}
$url = http_build_url($urlparams);
//http_build_url() is in PECL, you might need to manually rebuild the 
//url by looping through it's components:
/*
   $url=(isset($urlparams["scheme"])?$urlparams["scheme"]."://":"").
       (isset($urlparams["user"])?$urlparams["user"]:"").
       (isset($urlparams["pass"])? ":".$urlparams["pass"]:"").
       (isset($urlparams["user"])?"@":"").
       (isset($urlparams["host"])?$urlparams["host"]:"").
       (isset($urlparams["port"])?":".$urlparams["port"]:"").
       (isset($urlparams["path"])?$urlparams["path"]:"").
       (isset($urlparams["query"])?"?".$urlparams["query"]:"").
       (isset($urlparams["fragment"])?"#".$urlparams["fragment"]:""); 
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文