题
我在导航上有这些标签:
<li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks") echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings") echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
而且我只想在登录管理员时向他们展示:
if ($_COOKIE['custid'] == "admin") {
echo "Customers";
echo "Trunks";
echo "Settings";
}
我该如何结合其中两个脚本???
解决方案
<?php
if ($_COOKIE['custid'] == "admin") { ?>
<li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks") echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings") echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
<?php } ?>
很简单,把它放在另一个...
其他提示
将“ cookie中的管理员”问题视为一个单独的问题...
<?php if($admin): ?>
<li<?php if ($thisPage=="Customers"): ?> class="current"<?php endif; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks"): ?> class="current"<?php endif; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings"): ?> class="current"<?php endif; ?>><a href="/settings/">Settings</a></li>
<?php endif; ?>
PHP的内联语法比在HTML中使用{}和Echos要好得多
不确定您的意思,而是:
<?php
if ($_COOKIE['custid'] == "admin") { ?>
<li<?php if ($thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($thisPage=="Trunks") echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($thisPage=="Settings") echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
<?php } else { ?>
<li><a href="/customers/">Customers</a></li>
<li><a href="/trunks/">Trunks</a></li>
<li><a href="/settings/">Settings</a></li>
<?php } ?>
// OR
<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Customers") echo " class=\"current\""; ?>><a href="/customers/">Customers</a></li>
<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Trunks") echo " class=\"current\""; ?>><a href="/trunks/">Trunks</a></li>
<li<?php if ($_COOKIE['custid'] == "admin" && $thisPage=="Settings") echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>
我同意 @webdestroya 在有关帖子本身的评论中;您应该使用会话或类似而不是cookie来验证管理员状态。为了示例,我只是没有在这里更改它。
不隶属于 StackOverflow