如今愈來愈多的用到二級甚至多級菜單,前臺菜單的顯示,手動指定也愈來愈不能知足要求,因此,動態生成菜單是必須的 php
思路 + 示例代碼(以二級菜單爲例) html
先取出一級菜單內容值,接下來遍歷一級菜單,將其id當作本次檢索的parentid,將與之對應的二級菜單值獲取到, mysql
並加入到當前數組中(後臺) sql
二層循環,當獲取一個值時,檢查其對於的二級菜單項是否有數據,有的話,則輸出來,沒有則跳過(前臺) 數組
以PHP後臺爲例 htm
$res = mysql_query('*** where parentid = 0'); //獲取一級菜單內容 後臺
foreach($res as &$v) foreach
{ 循環
.$Tres = mysql_query('*** where parentid = ' . $v['id']); 遍歷
$v['T'] = $Tres;
}
前臺
<?php if($res != null): ?>
<?php foreach($res as $val): ?>
<li>外層html標籤</li>
<ul>
<?php if($res != null): ?>
<?php foreach($res as $val): ?>
<li>裏層html標籤</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
<?php endforeach; ?>
<?php endif; ?>
我的觀點,若是有不一樣的,歡迎你們提出