我正在尝试使用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循环?
哪种方式最好?
由于
答案 0 :(得分:1)
你应该有一个数组
$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>
答案 1 :(得分:1)
$gender = array('men','women');
$productlist = array('shirt','t-shirt','polo shirt','formal shoes','sport shoes','suit','underwear','socks','pants');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product) {
<li><a href="ProductsList.php"><?php echo $product; ?></a></li>
}
echo "</ul>";
}
答案 2 :(得分:0)
$gender = array('men','women');
$productlist = array('shirt'=>'shirt_url.php','t-shirt'=>'t-shirt_url.php','polo shirt'=>'polo_shirt.php');
foreach($gender as $individual) {
echo "<h2>{$individual}</h2>";
echo "<ul>";
foreach($productlist as $product_key=>$product_url) {
?>
<li><a href="<?php echo $product_url; ?>"><?php echo $product_key; ?></a></li>
<?php
}
echo "</ul>";
}