dscbxou1900343 2009-09-19 09:53
浏览 348
已采纳

PHP - 发送脚本的url-address

I'm creating pagging for some data. For example, I have a page:

http://example.com/news.php?type=bla&smth=bla

There are I have a list of news with links to another pages. Link to the first page is:

http://example.com/news.php?type=bla&smth=bla&page=1

Here is script, which creates pages links:

print '<a href="?'.$_SERVER['QUERY_STRING'].'&page=1"><<</a>'; 

But after clicking to another pages the link URL is very large and it looks like:

http://example.com/news.php?type=bla&smth=bla&page=1&page=2&page=1&page=3

How can I change that?

  • 写回答

5条回答 默认 最新

  • dpkt31779 2009-09-19 10:05
    关注

    You are just append the new paremeter to the old ones but you don’t replace it if already existing. So you rather need to merge the old query string with the new one:

    // either by merging both arrays
    $query = array_merge($_GET, array('page'=>1));
    // or by the union of both
    $query = array('page'=>1) + $_GET;
    // or by altering the array
    $query = $_GET;
    $query['page'] = 1;
    

    And PHP does already have a http_build_str that can build you a query string from an associative array:

    print '<a href="?' . htmlspecialchars(http_build_str($query)) . '">&lt;&lt;</a>';
    

    Edit    Here’s an alternative definition of http_build_str:

    if (!function_exists('http_build_str')) {
        function http_build_str($query, $prefix='', $arg_separator='') {
            if (!is_array($query)) {
                return null;
            }
            if ($arg_separator == '') {
                $arg_separator = ini_get('arg_separator.output');
            }
            $args = array();
            foreach ($query as $key => $val) {
                $name = $prefix.$key;
                if (!is_numeric($name)) {
                    $args[] = rawurlencode($name).'='.urlencode($val);
                }
            }
            return implode($arg_separator, $args);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭