用于设置活动菜单项的 switch 语句

发布于 2024-10-31 22:00:10 字数 2131 浏览 1 评论 0原文

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

$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>

Ok, so i thought I could do this like this, but perhaps not. Is this possible ?

$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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-11-07 22:00:10

将该行替换

$active = 'class="bob"'

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

Replace the line

$active = 'class="bob"'

by

$active = array("","","","","","","");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文