切换语句以设置活动菜单项

时间:2011-04-14 10:31:35

标签: php html css switch-statement

好的,所以我想我可以这样做,但也许不是。这可能吗?

$url = $_SERVER['REQUEST_URI'];

$active = 'class="bob"';

switch($url){

    case('/index.php'):
        $active[0] = ' class="active"';
    break;

    case('/about.php'):
    case('/our-customers.php'):
    case('/our-partners.php'):
    case('/our-clients.php'):
    case('/our-standards.php'):
    case('/our-awards.php'):
        $active[1] = ' class="active"';
    break;

    case('/solutions.php'):
        $active[2] = ' class="active"';
    break;

    case('/services.php'):
        $active[3] = ' class="active"';
    break;

    case('/sustainability.php'):
    case('/what-are-we-doing.php'):
    case('/what-can-you-do.php'):
    case('/what-we-support.php'):
    case('/references.php'):
        $active[4] = ' class="active"';
    break;

    case('/healthcare.php'):
        $active[5] = ' class="active"';
    break;

    case('/blog/'):
        $active[6] = ' class="active"';
    break;

    case('/contact.php'):
        $active[7] = ' class="active"';
    break;
}

>

?>

<nav>
  <ul class="clearfix">
    <li <?php echo $active[0] ?> id="first"><a href="index.php" class="Bold">Home</a></li>
    <li <?php echo $active[1] ?>><a href="about.php" class="Bold">About</a></li>
    <li <?php echo $active[2] ?>><a href="solutions.php" class="Bold">Solutions</a></li>
    <li <?php echo $active[3] ?>><a href="services.php" class="Bold">Services</a></li>
    <li <?php echo $active[4] ?>><a href="sustainability.php" class="Bold">Sustainability</a></li>
    <li <?php echo $active[5] ?>><a href="healthcare.php" class="Bold">Healthcare</a></li>
    <li <?php echo $active[6] ?>><a href="/blog/" class="Bold">Blog</a></li>
    <li <?php echo $active[7] ?> id="last"><a href="contact.php" class="Bold">Contact</a></li>
  </ul>
</nav>

1 个答案:

答案 0 :(得分:0)

替换

$active = 'class="bob"'

通过

$active = array("","","","","","","");