我正在尝试创建一个子导航,当脚本在适当的页面和子页面上找到自己时,它会回显“当前”。
我的问题是,当$subpage == $encoded_subpage_name?"current":""
比较时,带有空格的子导航名称总是相等的false
,因为$encoded_subpage_name
会有+
但$subpage
不会。
<?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 & 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>