I use a paging system like this:
<?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;
}
?>
I want to set css class="active"
to the menu item of the page i'm on.
It works if I print <li>
items like this:
<li><a href="?p=start" <?php if ($currentPageId == 1) {echo "class='active'";}else {} ?>>Start</a></li>
But I would like to use ternary operator instead. I tried this code but it doesn't work:
<li><a href="?p=start" <?php ($currentPageId == '1') ? 'class="active"' : '' ?>>Startsida</a></li>
Any idea why?
You are missing an echo:
<li><a href="?p=start" <?php echo (($currentPageId == '1') ? 'class="active"' : '') ?>>Startsida</a></li>
That should do the trick.
Addressing the second question:
<?php
if($something == true) {
echo "<div id='nav'>"."\n<br>".
"<ul>"."\n<br>".
'<li><a href="?p=start"'. (($currentPageId == '1') ? 'class="active"' : '') .'>Startsida</a></li>'."\n<br>".
'<li><a href="?p=customers" '. (($currentPageId == '1') ? 'class="active"' : '') .' >Customers</a></li>'."\n<br>".
"</ul>"."\n<br>".
"</div>"."\n<br>";
}
?>