我正在尝试使用 php 和 mysql 动态生成以下菜单
<ul id="prod_nav" class="clearfix">
<!-- top --> <li class="active"><a href="05-Pink-02-Category-List.html" class="top_link"><span class="down">Clothes</span></a>
<ul>
<h1> Men </h1>
<li><a href="ProductsList.php">Shirt </a></li>
<li><a href="ProductsList.php">T-shirt </a></li>
<li><a href="ProductsList.php">Polo shirt </a></li>
<li><a href="ProductsList.php">Formal shoes </a></li>
<li><a href="ProductsList.php">Sport shoes </a></li>
<li><a href="ProductsList.php">Suit </a></li>
<li><a href="ProductsList.php">Underwear </a></li>
<li><a href="ProductsList.php">Socks </a></li>
<li><a href="ProductsList.php">Pants </a></li>
</ul>
<ul>
<h1> Women </h1>
<li><a href="ProductsList.php">Shirt </a></li>
<li><a href="ProductsList.php">T-shirt </a></li>
<li><a href="ProductsList.php">Polo shirt </a></li>
<li><a href="ProductsList.php">High heel shoes </a></li>
<li><a href="ProductsList.php">Sport shoes </a></li>
<li><a href="ProductsList.php">Wedding clothes </a></li>
<li><a href="ProductsList.php">Underwear </a></li>
<li><a href="ProductsList.php">Leather </a></li>
<li><a href="ProductsList.php">Socks </a></li>
<li><a href="ProductsList.php">Pants </a></li>
</ul>
</li>
</ul>
但我不确定哪种方式是生成菜单的最佳原理? 我应该使用 while 循环和 if 或 Case 还是 for 循环?
哪种方式是最好的方式?
谢谢
最佳答案
你应该有一个数组
$a=array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
并像这样使用它:
<ul>
<h1>MEN</h1>
<?php foreach($a as $val) :?>
<li><a href="ProductsList.php"><?php echo $val; ?></a></li>
<?php endforeach ;?>
</ul>
<ul>
<h1>womaen</h1>
<?php foreach($a as $val) :?>
<li><a href="ProductsList.php"><?php echo $val; ?></a></li>
<?php endforeach ;?>
</ul>
关于php - 如何生成 3 级服务器端菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629786/