what's wrong with my php code?
<li><a class="<?php if($_GET['q']=='addons.html');echo 'current';?>" href="addons.html">test</a></li>
<li><a class="<?php if($_GET['q']=='technicalsupport.html');echo 'current';?>" href="technicalsupport.html">example</a></li>
<li><a class="<?php if($_GET['q']=='center.html');echo 'current';?>" href="center.html"&g开发者_高级运维t;center</a></li>
<li><a class="<?php if($_GET['q']=='about.html');echo 'current';?>" href="about.html">about</a></li>
why those are all add the "current" the the current page? i want to add the class="current"
only to the current page
Remove the ;
after the closing )
of your if
statement. i.e.
<?php if($_GET['q']=='about.html');echo 'current';?>
…becomes:
<?php if($_GET['q']=='about.html') echo 'current';?>
I agree with bradley.ayers, however I would also enclose the entire class tag within the If condition, like so.
<a <?php if($_GET['q']=='addons.html') echo 'class="current" ';?>href="addons.html">
This way all anchors whereby the condition is not true don't end up containing an empty class=""
tag
First, check if $_GET['q'] exists, so you won't get an "undefined index" notice.
<li>
<a href="addons.html" <?php echo (isset($_GET['q']) && $_GET['q'] == 'addons.html' ? 'class="current"' : ''); ?>>test</a>
</li>
I would do it a bit cleaner like so:
<?php echo ($_GET['q']=='about.html') ? 'current': ''; ?>
or even optimize it into a function:
<?php
function set_current($page){
return ($_GET['q'] == $page) ? 'current': '';
}
?>
and your lines then becoming cleaner:
<?php echo set_current('about.html'); ?>
精彩评论