duanqian9503 2012-07-22 06:43
浏览 33
已采纳

PHP:如何使用$ _GET从字面上读取参数字符?

I am trying to create a subnav that echos "current" when the script finds itself on the appropriate page and subpage.

My issue is that when $subpage == $encoded_subpage_name?"current":"" is compared, subnav names with spaces always equal false because $encoded_subpage_name will have a + but $subpage won't.

<?php

    if (isset($_GET['p'])) {
        $page = $_GET['p'];
    } else {
        $page = "";
    }
    if (isset($_GET['subp'])) {
        $subpage = $_GET['subp'];
    } else {
        $subpage = "";
    }

    $subpages = array(
        "powered by company",
        "text messaging",
        "the app",
        "NFC",
        "your membership",
        "faq",
        "privacy policy",
        "terms &amp; conditions"
    );


    function buildSubNav($page, $subpages) {
        foreach ($subpages as $subpage) {
            $encoded_subpage_name = urlencode($subpage);
            echo '<li class="' . selected_subpage($encoded_subpage_name) . '"><a href="?p=' . $page . '&subp=' . $encoded_subpage_name . '"> ' . $subpage . '</a></li>';
        }
    }
    function selected_subpage($encoded_subpage_name) {
        global $subpage;
        $subpage = ($subpage);
        echo $subpage;
        return $subpage == $encoded_subpage_name?"current":"";
    }


?>
<ul>
    <?php buildSubNav($page, $subpages); ?>
</ul>
  • 写回答

3条回答 默认 最新

  • dongzhao5970 2012-07-22 06:47
    关注

    The problem you're having is that you're encoding in the middle of your work instead of at the end when you're actually outputting. Stop doing that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!