I have an array below:
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
(
)
)
)
)
)
I have a php recursive function to convert this array into html menu:
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;
}
And this function will output HTML tree:
<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>
But I need to get something like that: _http://vasterad.com/plugins/responsive_css3_mega_menu/#
Separated by columns as I have a columns field in my database. How can I fix my recursion function? Thanks!
Either i'm misunderstanding the issue, or you are simply looking for markup of your generated html code. After a quick glance it looks like your recursive function is fine... it'll just look weird. The layout and looks isn't really something
PHP
handles. For this you want to usecss
and just regularHTML
.One remark on the actual code in the recursive function ... Inline HTML is not really the way to go. If you would work with a proper view, i do believe you'll be able to identify your 'problem' a bit better.