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)
但我觉得必须有一个更好,更清洁的方法来做到这一点......任何想法
当然,
将所有链接及其文本放在数组中.
然后迭代数组以执行输出.
如果输出类似于您知道它没有活动功能,则可以继续下一步.
引入检查映射链接的当前请求,如果是,则将类添加到输出.
您已使用迭代解决了问题.通过将数据放入可迭代(可循环)的结构中,将相同的方法应用于相同的数据.不要重复代码,重复数据.希望这可以帮助.