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条)

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本