下面我有一個數組:生成鄰接表megamenu
Array
(
[0] => Array
(
[id] => 1
[name] => Electronics
[parent] => 0
[description] => Large amount of electronics in our store
[columns] => 6
[products] => 5
[subcat] => Array
(
[0] => Array
(
[id] => 7
[name] => Moble phones
[parent] => 1
[description] =>
[columns] => 0
[products] => 5
[subcat] => Array
(
)
)
[1] => Array
(
[id] => 16
[name] => Computers
[parent] => 1
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
[0] => Array
(
[id] => 37
[name] => Desktops
[parent] => 16
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
)
)
[1] => Array
(
[id] => 17
[name] => Car Electronics
[parent] => 16
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
[0] => Array
(
[id] => 43
[name] => GPS & Navigation
[parent] => 17
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
)
)
)
)
)
)
[2] => Array
(
[id] => 18
[name] => TV & Video
[parent] => 1
[description] =>
[columns] => 0
[products] => 2
[subcat] => Array
(
[0] => Array
(
[id] => 48
[name] => LED TVs
[parent] => 18
[description] =>
[columns] => 0
[products] => 2
[subcat] => Array
(
)
)
[1] => Array
(
[id] => 49
[name] => Plasma TVs
[parent] => 18
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
)
)
)
)
)
)
[1] => Array
(
[id] => 14
[name] => Video Games
[parent] => 0
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
[0] => Array
(
[id] => 30
[name] => Nintendo Wii
[parent] => 14
[description] =>
[columns] => 0
[products] => 0
[subcat] => Array
(
)
)
)
)
)
我有一個PHP遞歸函數來此數組轉換成HTML菜單:
private function buildNavHTML($nav, $tabs = "") {
$html = !strlen($tabs) ?
$tabs.'<ul class="navs">' :
$tabs.'<ul>';
foreach($nav as $page) {
$html .= $tabs." ".'<li>';
(isset($page['subcategories'][0])) ?
$html .= '<a class="subcat" href="cat?cid='.$page['id'].'">'.$page['name'].'</a>' :
$html .= '<a href="cat?cid='.$page['id'].'">'.$page['name'].'</a>';
if(isset($page['subcat'][0])) {
$html .= self::buildNavHTML($page['subcat'], $tabs." ");
}
$html .= '</li>';
}
$html .= $tabs.'</ul>';
return $html;
}
這個函數將輸出HTML樹:
<ul class="navs">
<li class="">
<a class="shop_subcat" href="cat?cid=1">Electronics</a>
<ul>
<li>
<a href="cat?cid=7">Moble phones</a>
</li>
<li>
<a class="shop_subcat" href="cat?cid=16">Computers</a>
<ul>
<li>
<a href="cat?cid=37">Desktops</a>
</li>
<li>
<a class="shop_subcat" href="cat?cid=17">Car Electronics</a>
<ul>
<li>
<a href="cat?cid=43">GPS & Navigation</a>
</li>
</ul>
</li>
</ul>
</li>
<li>
<a class="shop_subcat" href="cat?cid=18">TV & Video</a>
<ul>
<li>
<a href="cat?cid=48">LED TVs</a>
</li>
<li>
<a href="cat?cid=49">Plasma TVs</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="">
<a class="shop_subcat" href="cat?cid=14">Video Games</a>
<ul>
<li>
<a href="cat?cid=30">Nintendo Wii</a>
</li>
</ul>
</li>
</ul>
但我需要類似的東西:_http://vasterad.com/plugins/responsive_css3_mega_menu/#
由列分隔,因爲我在我的數據庫中有一個列字段。我怎樣才能修復我的遞歸函數? 謝謝!
我看到你的超級菜單的鏈接,你想要哪種類型的菜單包含兩種風格,首先是fullwidht和基本風格。 –