我使用这样的寻呼系统:
<?php
$p = $_GET['p'];
switch($p)
{
case "start":
$p = "pages/start.php";
$currentPageId = 1;
break;
case "customers":
$p = "pages/customers.php";
$currentPageId = 2;
break;
default:
$p = "pages/start.php";
$currentPageId = 1;
break;
}
?>
我想将 css 设置class="active"
为我所在页面的菜单项。
如果我打印<li>
这样的项目,它会起作用:
<li><a href="?p=start" <?php if ($currentPageId == 1) {echo "class='active'";}else {} ?>>Start</a></li>
但我想改用三元运算符。我尝试了这段代码,但它不起作用:
<li><a href="?p=start" <?php ($currentPageId == '1') ? 'class="active"' : '' ?>>Startsida</a></li>
知道为什么吗?
编辑
所以问题是我错过了一个echo
. 现在让我稍微扩展一下这个问题......
我需要将我的整个封装<ul>
在<?php ?>
标签内。所以我想要的是这样的:
echo "<div id='nav'>";
echo "<ul>";
echo "<li><a href='?p=start' /* ternary operator to match if the page I'm on is equal to $currentPageId as defined in the paging system (above), if so set class='active' else do nothing*/>Start</a></li>;
echo "<li><a href='?p=customers' /* ternary operator to match if the page I'm on is equal to $currentPageId as defined in the paging system (above), if so set class='active' else do nothing*/>Customers</a></li>;
echo "</ul>";
echo "</div>";
我需要这样做,因为我将根据if
语句显示链接..“如果用户是管理员,则显示此链接,否则不显示”...有人有解决方案吗?