是否有更简洁的方法来执行每页的侧边栏活动状态?

Mat*_*iby 2 html php navigation sidebar

我有这个侧边栏代码

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....
Run Code Online (Sandbox Code Playgroud)

ans我想在当前页面添加一个活动类,所以这就是我采用的方法

    <li><a href="/restaurant_pos"><span>Restaurant POS Systems</span></a>
<ul>
    <li><a <?php if($_SERVER['REQUEST_URI'] == '/restaurant_pos/'){
        echo "class='active' ";
    } 
    ?> href="/restaurant_pos">Restaurant POS Systems</a></li>
    <li><a 
        <?php if($_SERVER['REQUEST_URI'] == '/bar_nightclub_pos/'){
            echo "class='active' ";
        } 
        ?>
        href="/bar_nightclub_pos">Bar and Nightclub POS Systems</a></li>
    <li><a href="/bbq-restaurant-pos">BBQ Restaurant POS</a></li>
    <li><a href="/bowling-alley-pos-system">Bowling Alley Point of Sale</a></li>
    <li><a href="/cafe-pos">Cafe Point of Sale Systems</a></li>
    <li><a href="/chinese-restaurant-pos">Chinese Restaurant POS Systems</a></li>
    <li><a href="/fine-dining-pos">Fine Dining Point of Sale Systems</a></li>
    ....
    ....
Run Code Online (Sandbox Code Playgroud)

但我觉得必须有一个更好,更清洁的方法来做到这一点......任何想法

hak*_*kre 5

当然,

  1. 将所有链接及其文本放在数组中.

  2. 然后迭代数组以执行输出.

  3. 如果输出类似于您知道它没有活动功能,则可以继续下一步.

  4. 引入检查映射链接的当前请求,如果是,则将类添加到输出.

您已使用迭代解决了问题.通过将数据放入可迭代(可循环)的结构中,将相同的方法应用于相同的数据.不要重复代码,重复数据.希望这可以帮助.

  • 不确定你的意思把它们放在一个数组中... $ pages = array("restaurant_pos"=>"/ restaurant_pos /","bar_nightclub_pos"=>"/ bar_nightclub_pos /"); 不确定这是如何有用的 (2认同)