Jump to content

[SOLVED] php if not working


dennismonsewicz

Recommended Posts

I have this if statement

 

if($action == 'manage') {
$active = 'class="active"';
} elseif($action == 'settings') {
$active = 'class="active"';
} elseif($action == 'write') {
$active = 'class="active"';
} else {
$active = '';
}

 

it is supposed to write the class to the link according to which action is in place... and its not working

 

css

 

<div class="navigation">
<ul>
	<li><a href="index.php?action=write" <?php echo $active; ?>>WRITE</a></li>
	<li><a href="index.php?action=manage" <?php echo $active; ?>>MANAGE</a></li>
	<li><a href="index.php?action=settings" <?php echo $active; ?>>SETTINGS</a></li>
</ul>			
</div>

Link to comment
https://forums.phpfreaks.com/topic/143508-solved-php-if-not-working/
Share on other sites

I took out the initial IF statement and replaced everything with this:

 

<div class="navigation">
			<ul>
				<li><a href="index.php?action=write" <?php if($action == 'write') { ?> class="active" <?php } ?>>WRITE</a></li>
				<li><a href="index.php?action=manage" <?php if($action == 'manage') { ?> class="active" <?php } ?>>MANAGE</a></li>
				<li><a href="index.php?action=settings" <?php if($action == 'settings') { ?> class="active" <?php } ?>>SETTINGS</a></li>
			</ul>			
	</div>

 

It works like a charm

Even better....

 

<div class="navigation">
  <ul>
    <li><a href="index.php?action=write" <?php echo $action == 'write' ? 'class="active"' : ''; ?>>WRITE</a></li>
    <li><a href="index.php?action=manage" <?php echo $action == 'manage' ? 'class="active"' : ''; ?>>MANAGE</a></li>
    <li><a href="index.php?action=settings" <?php echo $action == 'settings' ? 'class="active"' : ''; ?>>SETTINGS</a></li>
  </ul>			
</div>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.