1

In bootstrap 3 we have this <li> class set to active in order to display the current page highlighted on the navbar

<li class="active">
     <a href="linkaddress.html">Link</a>
</li>

the problem is when you are including the menu via include includes/header.php; on all your pages. i cant figure out how to put together a switch statement on the $actual_link and bring back some sort of call to insert the class active in the right place. this my so far attempt and im honestly stuck because i feel there is a better way. how can i can i put the li class to active with this switch

$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$HTTPHost = $_SERVER[HTTP_HOST];
switch($actual_link)
{
case "http://{$HTTPHost}/index.php" || 
    "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":

//setToActive
    break;
case "http://{$HTTPHost}/warrants.php" || 
    "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_warrants":
 //setToActive     
    break;
case "http://{$HTTPHost}/faq.php" || 
    "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_faq":
 //setToActive 
    break;
case "http://{$HTTPHost}/aboutus.php" || 
    "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_aboutus":
  //setToActive  
    break;
}

?>

Gunr Jesra
  • 29
  • 5

2 Answers2

0

In your pages before including header set a variable to indicate the current page. For example in index page:

$active = 'index';
include('includes/header.php');

Now in your header.php while creating links use something like

<?php global $active ?>
<li <?php if( isset($active) && $active == 'index') echo 'class="active"'; ?>>
     <a href="index.php">Index</a>
</li>
Konsole
  • 3,447
  • 3
  • 29
  • 39
  • looks like you forgot to put braces around echo – Gunr Jesra Sep 01 '13 at 09:06
  • there is a mistake, is you set one to active, they all expect active so they all get highlighted, its not working but i guess i can do `index1, index2` – Gunr Jesra Sep 01 '13 at 09:10
  • Glad that you liked the answer :). Braces are not mandatory for only a single statement inside if or loops. Refer this thread http://stackoverflow.com/questions/8726339/php-if-else-for-foreach-while-without-curly-braces – Konsole Sep 01 '13 at 09:11
  • oh i see now -_- i feel stupid, index was for the index, and so on each one should have a name different lol sorry – Gunr Jesra Sep 01 '13 at 09:22
0

you can't specify multiple expression in a case statement like this

case "http://{$HTTPHost}/index.php" || 
    "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":

the above expression evaluates to true.

use the following synatax

case "http://{$HTTPHost}/index.php":
case "http://{$_SERVER[HTTP_HOST]}/admin.php?edit_home":
     //statements 
     break;
DevZer0
  • 13,433
  • 7
  • 27
  • 51