开发者

php help hiding navigation with cookie

I have these tabs on my navigation:

<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 ($this开发者_开发百科Page=="Settings")  echo " class=\"current\""; ?>><a href="/settings/">Settings</a></li>   

and I only want to show them when admin is logged in:

if ($_COOKIE['custid'] == "admin") {

echo "Customers";
echo "Trunks";
echo "Settings";

}

How can I combine the two of these scripts???


Treating the "admin in cookie" issue as a separate issue...

<?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's inline syntax is much nicer than using {} and echos inside html


<?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 } ?>

Pretty simple, put it inside the other one...


Not exactly sure what you mean, but:

<?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>

And I agree with @webdestroya in the comments on the post itself; you should use a session or similar instead of a cookie to verify administrator status. I just didn't change it here for the sake of the example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜