【问题标题】:Additional elements to URLS?URL 的附加元素?
【发布时间】:2013-07-25 11:34:01
【问题描述】:

我不确定术语是什么,但基本上我有一个使用“tag-it”系统的网站,目前你可以点击标签,它会引导用户

topics.php?tags=example

我的问题是需要什么样的脚本或编码才能添加额外的链接?

topics.php?tags=example&tags=example2

topics.php?tags=example+example2

这是我的网站如何链接到标签的代码。

header("Location: topics.php?tags={$t}");

<a href="topics.php?tags=<?php echo $fetch_name->tags; ?>"><?php echo strtolower($fetch_name->tags);?></a>

感谢任何提示或提示。

【问题讨论】:

    标签: php tag-it


    【解决方案1】:

    虽然可以将标签作为数组传递,但实际上不能将标签作为 GET 参数传递两次

    topics.php?tags[]=example&tags[]=example2
    

    假设这是你想要的尝试

    $string = "topics.php?";
    foreach($tags as $t)
    {
        $string .= "tag[]=$t&";
    }
    $string = substr($string, 0, -1);
    

    我们遍历数组连接值到我们的 $string。最后一行删除了一个额外的 & 符号,它将在最后一次迭代之后出现

    还有另一个选项看起来有点脏,但根据您的需要可能会更好

    $string = "topics.php?tag[]=" . implode($tags, "&tag[]=");
    

    注意只要保证 tags 数组不为空

    【讨论】:

    • 这就是我要发布的内容
    • 构建这样一个 URL 查询字符串的“正确”方法是使用http_build_query():http_build_query( array( 'tags' =&gt; array( 'example-tag-1', 'example-tag-2' ) ) );
    • 感谢方便的功能,可惜我以前从未遇到过它
    【解决方案2】:

    topics.php?tags=example&amp;tags=example2
    将在后端中断;

    您必须将数据分配给一个变量:

    topics.php?tags=example+example2

    看起来不错,您可以通过+ 符号在后端explode 访问它:

    //toplics.php
    <?php
        ...
        $tags = urlencode($_GET['tags']);
        $tags_arr = explode('+', $tags); // array of all tags
    
        $current_tags = ""; //make this accessible in the view;
        if($tags){
             $current_tags = $tags ."+"; 
        }
       //show your data
    ?>
    

    编辑: 您可以创建前端标签:

    <a href="topics.php?tags=<?php echo $current_tags ;?>horror">
        horror
    </a>
    

    【讨论】:

    • 我不确定加号是不是一个好的选择,如果我没记错的话,它是一个特殊的 url 字符。
    • 是的,加号是空格的特殊 url 字符 :)
    • 感谢您的帖子,我目前已经使用 $_GET['tags'] 部分设置了它,我唯一的困惑是如果我有电影恐怖作为标签,我可以继续“电影”,然后单击“恐怖”并将其添加到 URL。我以 + 为例,因为我看到 SO 使用该系统。
    • + 符号很好,因为它使 url 在当前场景下更具可读性,您始终可以在后端对其进行解码。
    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2021-10-26
    • 2015-06-22
    • 2021-11-27
    • 2017-01-24
    • 2020-03-17
    • 1970-01-01
    相关资源
    最近更新 更多