/    Sign up×
Community /Pin to ProfileBookmark

Multidimensional Output help.

I have a multi-array that is about 5/6 levels deep, I would like to
alter the way that my output is but am losing faith. Currently I use
this loop

[code=php]
function tree_menu($array, $indent=0) {
$prefix = “”;
for ($i=0; $i<$indent; $i++) {
$prefix .= ” “;
}
while (list ($key, $value) = each($array)) {
if (is_array($value)) {
echo $prefix . “<a class=”parent” href=”#”>” . $key . “</a>nr”;
tree_menu($value, $indent + 1);
} else {
echo $prefix . “<li><a href='” . $value . “‘>” . $key . “</a></li>nrt”;
}
}
}[/code]

to output something that looks a lot like this.

[code=html]
<a class=”parent” href=”#”>Products</a>

<a class=”parent” href=”#”>Lighting</a>

<a class=”parent” href=”#”>PR Lighting</a>

<li><a href=’prl-ar.php’>Architectural</a></li>

<li><a href=’prl-cp.php’>Centerpieces</a></li>
<li><a href=’prl-dj.php’>DJ Pro Series</a></li>

<a class=”parent” href=”#”>Theatrical</a>

<li><a href=’prl-fs.php’>Followspots</a></li>

<a class=”parent” href=”#”>Moving Yoke</a>

<li><a href=’prl-xl.php’>XL Series</a></li>

<li><a href=’prl-xs.php’>XS Series</a></li>

<li><a href=’prl-pt.php’>Pilot Series</a></li>

<li><a href=’prl-so.php’>Solo Series</a></li>

<a class=”parent” href=”#”>Omni Lighting</a>
<li><a href=’lit-ar.php’>Architectural</a></li>

<li><a href=’lit-cp.php’>Centerpieces</a></li>

<a class=”parent” href=”#”>Effect Lighting</a>

<li><a href=’lit-ad.php’>Advancer Series</a></li>

<li><a href=’lit-as.php’>American Series</a></li>

<li><a href=’lit-ss.php’>Star Series</a></li>

<li><a href=’lit-fo.php’>Fiber Optics</a></li>

<li><a href=’lit-il.php’>Intelligent</a></li>

<li><a href=’lit-sl.php’>Searchlights</a></li>
<li><a href=’lit-se.php’>Special Effects</a></li>

<li><a href=’lit-uv.php’>UV Fictures</a></li>

<li><a href=’lit-st.php’>Stands</a></li>

<a class=”parent” href=”#”>LED</a>

<li><a href=’led-fx.php’>Effects</a></li>

<li><a href=’led-pc.php’>Par Cans</a></li>

<li><a href=’led-my.php’>Moving Yoke</a></li>

<li><a href=’led-sp.php’>Strips</a></li>

<li><a href=’led-ps.php’>Panels</a></li>
<li><a href=’led-vw.php’>Video Walls</a></li>

… conct …

[/code]

I am using this as a nav menu function though and need to
alter my loop to support levels and etc and its getting a
bit past my level, would someone mind helping me tailor the
output to something more like this?

[code=html]
<ul class=”level1″><li><a class=”parent” href=”#”>Products</a><ul class=”level2″><li><a class=”parent” href=”#”>Lighting</a><ul class=”level3″><li><a class=”parent” href=”#”>PR Lighting</a><ul class=”level4″><li><a class=”parent” href=”#”>Theatrical</a><ul class=”level5″><li><a href=”<?=$light;?>prl-fs.php”>Followspots</a></li></ul></li> <li><a href=”http://omnisistem.com/layers/lighting/prl-ar.php”>Architectural</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-cp.php”>Centerpieces</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-dp.php”>DJ Pro Series</a></li>
<li>
<a class=”parent” href=”#”>Moving Yoke</a>

<ul class=”parent”>
<li><a href=”http://omnisistem.com/layers/lighting/prl-xl.php”>XL Series</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-xs.php”>XS Series</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-pt.php”>Pilot Series</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-so.php”>Solo Series</a></li>
</ul>
</li>
<li><a href=”http://omnisistem.com/layers/lighting/prl-sl.php”>Search Lights</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Omni Lighting</a>

<ul class=”level4″>

<li>
<a class=”parent” href=”#”>Theatrical</a>
<ul class=”level5″>
<li><a href=”http://omnisistem.com/layers/lighting/lit-fs.php”>Followspots</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-pc.php”>Stage Lights</a></li>
</ul>
</li>

<li><a href=”http://omnisistem.com/layers/lighting/lit-ar.php”>Architectural</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-cp.php”>Centerpieces</a></li>

<li>
<a class=”parent” href=”#”>Effect Lighting</a>
<ul class=”level5″>
<li><a href=”http://omnisistem.com/layers/lighting/lit-ad.php”>Advancer Series</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-as.php”>American Series</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-ss.php”>Star Series</a></li>
</ul>
</li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-fo.php”>Fiber Optics</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-il.php”>Intelligent</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-sl.php”>Searchlights</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-se.php”>Special Effects</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-uv.php”>UV Fixtures</a></li>
<li><a href=”http://omnisistem.com/layers/lighting/lit-st.php”>Stands</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>L.E.D.</a>

<ul class=”level3″>
<li><a href=”http://omnisistem.com/layers/led/led-fx.php”>Effects</a></li>
<!– <li><a href=”http://omnisistem.com/layers//layers/led/led-ll.php”> Lamps</a></li> –>
<li><a href=”http://omnisistem.com/layers/led/led-pc.php”>Par Cans</a></li>
<li><a href=”http://omnisistem.com/layers/led/led-my.php”>Moving Yoke</a></li>
<li><a href=”http://omnisistem.com/layers/led/led-sp.php”>Strips</a></li>
<li><a href=”http://omnisistem.com/layers/led/led-ps.php”>Panels</a></li>
<li><a href=”http://omnisistem.com/layers/led/led-vw.php”>Video Walls</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Lasers</a>

<ul class=”level3″>
<li><a href=”http://omnisistem.com/layers/lasers/lsr-pr.php”>Professional</a></li>
<li><a href=”http://omnisistem.com/layers/lasers/lsr-sp.php”>Semi-Professional</a></li>
<li><a href=”http://omnisistem.com/layers/lasers/lsr-dc.php”>DJ &amp; Club</a></li>
<li><a href=”http://omnisistem.com/layers/lasers/lsr-la.php”>Accessories</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Omni Cables</a>

<ul class=”level3″>
<li><a href=”http://omnisistem.com/layers/cable/cbl-al.php”>All Cables</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Omni Cases</a>

<ul class=”level3″>
<li><a href=”http://omnisistem.com/layers/case/css-hd.php”>Hard</a></li>
<li><a href=”http://omnisistem.com/layers/case/css-sf.php”>Soft</a></li>
<li><a href=”http://omnisistem.com/layers/case/css-rd.php”>Road</a></li>
<li><a href=”http://omnisistem.com/layers/case/css-fl.php”>Flight</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Omni Sound</a>

<ul class=”level3″>
<li><a href=”http://omnisistem.com/layers/sound/snd-ra.php”>PA Systems</a></li>

<li><a href=”http://omnisistem.com/layers/sound/snd-es.php”>SS/SB Series</a></li>
<li><a href=”http://omnisistem.com/layers/sound/snd-ej.php”>EJ Series</a></li>

<li><a href=”http://omnisistem.com/layers/sound/snd-us.php”>U Series</a></li>
<li><a href=”http://omnisistem.com/layers/sound/snd-xi.php”>Xi Series</a></li>
<li><a href=”http://omnisistem.com/layers/sound/snd-el.php”>Electronics</a></li>
</ul>
</li>
<li>
<a class=”parent” href=”#”>Truss Sistem</a>

[/code]

You can see that each UL class has a level based on where it is in the array
and though it is in there, I do not use the prefix in my code, so it can
be used for incrementations if need be. The biggest part is correctly
wrapping each “level” in its own <ul><li> </li></ul> tags to segregate.

I might change my array to also hold the value of the current paths,
aka [url]http://omnisistem.com/layer/lighting/[/url] But am not sure the most
appropriate place to include it, suggestions appreciated.

-Jd

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscSep 22.2009 — It would be super if you could post the actual array you are dealing with, too, that way we could test it using the exact same data. I can, however, tell you that if you want to make a new unordered list, then you need to echo a new ul and a closing ul at the beginning and end of your function, respectively, like so:


[code=php]
function tree_menu($array, $indent=0) {
$prefix = "";
for ($i=0; $i<$indent; $i++) {
$prefix .= " ";
}
echo $prefix."<ul class="level".($i+1)."">n";
while (list ($key, $value) = each($array)) {
if (is_array($value)) {
echo $prefix . "<a class="parent" href="#">" . $key . "</a>nr";
tree_menu($value, $indent + 1);
} else {
echo $prefix . " <li><a href='" . $value . "'>" . $key . "</a></li>nrt";
}
}
echo $prefix."</ul>n";
}
[/code]


I modified the function now so that it may give you the proper classname too, but I may have done it too sloppily - give it a try!
Copy linkTweet thisAlerts:
@aj_nscSep 22.2009 — wrong thread...
Copy linkTweet thisAlerts:
@ehimeauthorSep 22.2009 — Rewrote the menu function, working better but I still can't hit level five ? :sadpanda:

[code=php]
//output menu function
function outputMenu ($array, $level) {
global $levelMarker;

foreach ($array as $key => $value) {
if (is_array($value)) {
echo "t<ul class='level" . $levelMarker . "'>rnt<li><a class="parent" href="#">" . $key . "</a>nr";
outputMenu($value, $levelMarker++);

} else {
echo "tt<li><a href='" . $value . "'>" . $key . "</a></li>nr";
}
}
echo "tt</li></ul>rn";
$levelMarker--;
}

//output menu settings
$levelMarker = 1;

//output menu
outputMenu($menu, $level);
[/code]


output is like this now.
[code=html]
<ul class='level1'>
<li><a class="parent" href="#">Products</a>

<ul class='level2'>
<li><a class="parent" href="#">Lighting</a>

<ul class='level3'>
<li><a class="parent" href="#">PR Lighting</a>
<li><a href='prl-ar.php'>Architectural</a></li>

<li><a href='prl-cp.php'>Centerpieces</a></li>

<li><a href='prl-dj.php'>DJ Pro Series</a></li>

<ul class='level4'>
<li><a class="parent" href="#">Theatrical</a>
<li><a href='prl-fs.php'>Followspots</a></li>

</li></ul>
<ul class='level4'>
<li><a class="parent" href="#">Moving Yoke</a>

<li><a href='prl-xl.php'>XL Series</a></li>

<li><a href='prl-xs.php'>XS Series</a></li>

<li><a href='prl-pt.php'>Pilot Series</a></li>

<li><a href='prl-so.php'>Solo Series</a></li>

</li></ul>
</li></ul>
<ul class='level3'>
<li><a class="parent" href="#">Omni Lighting</a>

<li><a href='lit-ar.php'>Architectural</a></li>

<li><a href='lit-cp.php'>Centerpieces</a></li>

<ul class='level4'>
<li><a class="parent" href="#">Effect Lighting</a>
<li><a href='lit-ad.php'>Advancer Series</a></li>

<li><a href='lit-as.php'>American Series</a></li>

<li><a href='lit-ss.php'>Star Series</a></li>

</li></ul>
<li><a href='lit-fo.php'>Fiber Optics</a></li>
<li><a href='lit-il.php'>Intelligent</a></li>

<li><a href='lit-sl.php'>Searchlights</a></li>

<li><a href='lit-se.php'>Special Effects</a></li>

<li><a href='lit-uv.php'>UV Fictures</a></li>

<li><a href='lit-st.php'>Stands</a></li>

</li></ul>
</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">LED</a>

<li><a href='led-fx.php'>Effects</a></li>
<li><a href='led-pc.php'>Par Cans</a></li>

<li><a href='led-my.php'>Moving Yoke</a></li>

<li><a href='led-sp.php'>Strips</a></li>

<li><a href='led-ps.php'>Panels</a></li>

<li><a href='led-vw.php'>Video Walls</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Lasers</a>

<li><a href='lsr-pr.php'>Professional</a></li>

<li><a href='lsr-sp.php'>Semi-Professional</a></li>
<li><a href='lsr-dj.php'>DJ & Club</a></li>

<li><a href='lsr-la.php'>Accessories</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Omni Cables</a>
<li><a href='cbl-al.php'>All Cables</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Omni Cases</a>

<li><a href='css-hd.php'>Hard</a></li>

<li><a href='css-sf.php'>Soft</a></li>

<li><a href='css-rd.php'>Road</a></li>

<li><a href='css-fl.php'>Flight</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Omni Sound</a>
<li><a href='snd-ra.php'>PA Systems</a></li>

<li><a href='snd-es.php'>SS/SB Series</a></li>

<li><a href='snd-ej.php'>EJ Series</a></li>

<li><a href='snd-us.php'>U Series</a></li>

<li><a href='snd-xi.php'>Xi Series</a></li>

<li><a href='snd-el.php'>Electronics</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Truss Sistem</a>

<li><a href='trs-cl.php'>Clamps</a></li>
<li><a href='trs-tr.php'>Truss</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Mirror Products</a>

<li><a href='mrr-bl.php'>Mirror Balls</a></li>

<li><a href='mrr-cl.php'>Mirror Cylinders</a></li>

<li><a href='mrr-mr.php'>Motors</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Special Effects</a>

<li><a href='sfx-fh.php'>Fog/Haze</a></li>
<li><a href='sfx-bb.php'>Bubble</a></li>

<li><a href='sfx-sf.php'>Snow/Foam</a></li>

<li><a href='sfx-sf.php'>Accessories</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Elevators</a>

<li><a href='elv-ve.php'>Video Elevators</a></li>

<li><a href='elv-el.php'>Elevator Lifts</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Controllers</a>
<li><a href='ctl-wi.php'>Wireless</a></li>

<li><a href='ctl-dx.php'>DMX Controls</a></li>

<li><a href='ctl-dl.php'>DasLight</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">DJ Consoles</a>

<li><a href='cnl-st.php'>Stands</a></li>

</li></ul>
<ul class='level2'>
<li><a class="parent" href="#">Furniture</a>

<li><a href='frn-tb.php'>Tables</a></li>
<li><a href='frn-ch.php'>Chairs</a></li>

<li><a href='frn-bs.php'>Barstools</a></li>

</li></ul>
</li></ul>
</li></ul>
[/code]
Copy linkTweet thisAlerts:
@ehimeauthorSep 22.2009 — Here's the actual array

[code=php]
$menu = array(
"Products" => array (

"Lighting" => array( "PR Lighting" => array( "Architectural" => "prl-ar.php",
"Centerpieces" => "prl-cp.php",
"DJ Pro Series" => "prl-dj.php",
"Theatrical" => array( "Followspots" => "prl-fs.php",
),

"Moving Yoke" => array( "XL Series" => "prl-xl.php",
"XS Series" => "prl-xs.php",
"Pilot Series" => "prl-pt.php",
"Solo Series" => "prl-so.php",
),
),

"Omni Lighting" => array( "Architectural" => "lit-ar.php",
"Centerpieces" => "lit-cp.php",
"Effect Lighting" => array( "Advancer Series" => "lit-ad.php",
"American Series" => "lit-as.php",
"Star Series" => "lit-ss.php",
),

"Fiber Optics" => "lit-fo.php",
"Intelligent" => "lit-il.php",
"Searchlights" => "lit-sl.php",
"Special Effects" => "lit-se.php",
"UV Fictures" => "lit-uv.php",
"Stands" => "lit-st.php",
),
),

"LED" => array( "Effects" => "led-fx.php",
"Par Cans" => "led-pc.php",
"Moving Yoke" => "led-my.php",
"Strips" => "led-sp.php",
"Panels" => "led-ps.php",
"Video Walls" => "led-vw.php",
),

"Lasers" => array( "Professional" => "lsr-pr.php",
"Semi-Professional" => "lsr-sp.php",
"DJ &#38; Club" => "lsr-dj.php",
"Accessories" => "lsr-la.php",
),

"Omni Cables" => array( "All Cables" => "cbl-al.php",
),

"Omni Cases" => array( "Hard" => "css-hd.php",
"Soft" => "css-sf.php",
"Road" => "css-rd.php",
"Flight" => "css-fl.php",
),

"Omni Sound" => array( "PA Systems" => "snd-ra.php",
"SS/SB Series" => "snd-es.php",
"EJ Series" => "snd-ej.php",
"U Series" => "snd-us.php",
"Xi Series" => "snd-xi.php",
"Electronics" => "snd-el.php",
),

"Truss Sistem" => array( "Clamps" => "trs-cl.php",
"Truss" => "trs-tr.php",
),

"Mirror Products" => array( "Mirror Balls" => "mrr-bl.php",
"Mirror Cylinders" => "mrr-cl.php",
"Motors" => "mrr-mr.php",
),

"Special Effects" => array( "Fog/Haze" => "sfx-fh.php",
"Bubble" => "sfx-bb.php",
"Snow/Foam" => "sfx-sf.php",
"Accessories" => "sfx-sf.php",
),

"Elevators" => array( "Video Elevators" => "elv-ve.php",
"Elevator Lifts" => "elv-el.php",
),

"Controllers" => array( "Wireless" => "ctl-wi.php",
"DMX Controls" => "ctl-dx.php",
"DasLight" => "ctl-dl.php",
),

"DJ Consoles" => array( "Stands" => "cnl-st.php",
),

"Furniture" => array( "Tables" => "frn-tb.php",
"Chairs" => "frn-ch.php",
"Barstools" => "frn-bs.php",
),
),
);[/code]
Copy linkTweet thisAlerts:
@ehimeauthorSep 22.2009 — Cleaned it up a bit, still no fifth level though.

The fifth level kick in only a few times, if you

look at my first output code, and at my array

it happens at Theatrical, Moving Yoke, and

effects lighting.

There's weird output with my code too, I just

noticed that after Lighting, the recursion seems

to stop LED on are not (seemingly) properly

formated.

renovated code:
[code=php]
//output menu function
function outputMenu ($array, $level, $indent=0) {
global $levelMarker;

$prefix = "";
for ($i=0; $i<$indent; $i++) {
$prefix .= " ";
}

foreach ($array as $key => $value) {
if (is_array($value)) {
echo $prefix . "<ul class='level" . $levelMarker . "'>r";
echo $prefix . "<li><a class="parent" href="#">" . $key . "</a>r";
outputMenu($value, $indent++, $levelMarker++);

} else {
echo $prefix . "<li><a href='" . $value . "'>" . $key . "</a></li>r";
}
}
echo $prefix . "</li></ul>";
echo "rnrn"; # Orderly
$levelMarker--;
}

//output menu settings
$levelMarker = 1;

//output menu
outputMenu($menu, $level);
[/code]
Copy linkTweet thisAlerts:
@aj_nscSep 22.2009 — This might have nothing to do with it, but why are there commas showing up after the terminal array elements when you printed out the array above?
Copy linkTweet thisAlerts:
@criterion9Sep 22.2009 — What if you used a recursive function such as when php scans directory structures?

For instance the function accepts an array argument.

Then completes a single pass including the ul prefix.

Then checks to see if the element it is at in the loop is an array.

If so it calls the same function using that value as the new array.

This would allow you to increase and/or decrease the levels you wish to output without needing to change your code at all.
Copy linkTweet thisAlerts:
@ehimeauthorSep 22.2009 — This might have nothing to do with it, but why are there commas showing up after the terminal array elements when you printed out the array above?[/quote]

*shrug Not on my side? Can you tell me where you're seeing them?




What if you used a recursive function such as when php scans directory structures?

For instance the function accepts an array argument.

Then completes a single pass including the ul prefix.

Then checks to see if the element it is at in the loop is an array.

If so it calls the same function using that value as the new array.

This would allow you to increase and/or decrease the levels you wish to output without needing to change your code at all.[/quote]



Great idea, I think I get the general gist of it, but I'm not sure how

I would implement it. I just revised my code, by using the suggestion

above to include an extra UL, the flow looks good and I only get two

errors (end for non existant UL and LI tags), but I haven't plugged it

into the actual app to see what kinda horrors it'll cause with all these

extra UL class=parent tags i included. *scared

code as it sits:
[code=php]
//output menu function
function outputMenu ($array, $level, $indent=0) {
global $levelMarker;

$prefix = "";
for ($i=0; $i<$indent; $i++) {
$prefix .= " ";
}

echo $prefix . "<ul class="parent">rn";
foreach ($array as $key => $value) {
if (is_array($value)) {
echo $prefix . "<ul class='level" . $levelMarker . "'>r";
echo $prefix . "<li><a class="parent" href="#">" . $key . "</a>r";
outputMenu($value, $indent++, $levelMarker++);

} else {
echo $prefix . "<li><a href='" . $value . "'>" . $key . "</a></li>r";
}
}
echo $prefix . "</ul></li></ul>";
echo "rnrn"; # Orderly
$levelMarker--;
}

//output menu settings
$levelMarker = 1;

//output menu

outputMenu($menu, $level);
[/code]


Output:
[code=html]
<ul class="parent">
<ul class='level1'>
<li><a class="parent" href="#">Products</a>
<ul class="parent">
<ul class='level2'>
<li><a class="parent" href="#">Lighting</a>
<ul class="parent">
<ul class='level3'>
<li><a class="parent" href="#">PR Lighting</a>
<ul class="parent">
<li><a href='prl-ar.php'>Architectural</a></li>
<li><a href='prl-cp.php'>Centerpieces</a></li>
<li><a href='prl-dj.php'>DJ Pro Series</a></li>
<ul class='level4'>
<li><a class="parent" href="#">Theatrical</a>
<ul class="parent">
<li><a href='prl-fs.php'>Followspots</a></li>
</ul></li></ul>

<ul class='level4'>
<li><a class="parent" href="#">Moving Yoke</a>
<ul class="parent">
<li><a href='prl-xl.php'>XL Series</a></li>
<li><a href='prl-xs.php'>XS Series</a></li>
<li><a href='prl-pt.php'>Pilot Series</a></li>
<li><a href='prl-so.php'>Solo Series</a></li>
</ul></li></ul>

</ul></li></ul>

<ul class='level3'>
<li><a class="parent" href="#">Omni Lighting</a>
<ul class="parent">
<li><a href='lit-ar.php'>Architectural</a></li>
<li><a href='lit-cp.php'>Centerpieces</a></li>
<ul class='level4'>
<li><a class="parent" href="#">Effect Lighting</a>
<ul class="parent">
<li><a href='lit-ad.php'>Advancer Series</a></li>
<li><a href='lit-as.php'>American Series</a></li>
<li><a href='lit-ss.php'>Star Series</a></li>
</ul></li></ul>

<li><a href='lit-fo.php'>Fiber Optics</a></li>
<li><a href='lit-il.php'>Intelligent</a></li>
<li><a href='lit-sl.php'>Searchlights</a></li>
<li><a href='lit-se.php'>Special Effects</a></li>
<li><a href='lit-uv.php'>UV Fictures</a></li>
<li><a href='lit-st.php'>Stands</a></li>
</ul></li></ul>

</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">LED</a>
<ul class="parent">
<li><a href='led-fx.php'>Effects</a></li>
<li><a href='led-pc.php'>Par Cans</a></li>
<li><a href='led-my.php'>Moving Yoke</a></li>
<li><a href='led-sp.php'>Strips</a></li>
<li><a href='led-ps.php'>Panels</a></li>
<li><a href='led-vw.php'>Video Walls</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Lasers</a>
<ul class="parent">
<li><a href='lsr-pr.php'>Professional</a></li>
<li><a href='lsr-sp.php'>Semi-Professional</a></li>
<li><a href='lsr-dj.php'>DJ &#38; Club</a></li>
<li><a href='lsr-la.php'>Accessories</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Omni Cables</a>
<ul class="parent">
<li><a href='cbl-al.php'>All Cables</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Omni Cases</a>
<ul class="parent">
<li><a href='css-hd.php'>Hard</a></li>
<li><a href='css-sf.php'>Soft</a></li>
<li><a href='css-rd.php'>Road</a></li>
<li><a href='css-fl.php'>Flight</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Omni Sound</a>
<ul class="parent">
<li><a href='snd-ra.php'>PA Systems</a></li>
<li><a href='snd-es.php'>SS/SB Series</a></li>
<li><a href='snd-ej.php'>EJ Series</a></li>
<li><a href='snd-us.php'>U Series</a></li>
<li><a href='snd-xi.php'>Xi Series</a></li>
<li><a href='snd-el.php'>Electronics</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Truss Sistem</a>
<ul class="parent">
<li><a href='trs-cl.php'>Clamps</a></li>
<li><a href='trs-tr.php'>Truss</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Mirror Products</a>
<ul class="parent">
<li><a href='mrr-bl.php'>Mirror Balls</a></li>
<li><a href='mrr-cl.php'>Mirror Cylinders</a></li>
<li><a href='mrr-mr.php'>Motors</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Special Effects</a>
<ul class="parent">
<li><a href='sfx-fh.php'>Fog/Haze</a></li>
<li><a href='sfx-bb.php'>Bubble</a></li>
<li><a href='sfx-sf.php'>Snow/Foam</a></li>
<li><a href='sfx-sf.php'>Accessories</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Elevators</a>
<ul class="parent">
<li><a href='elv-ve.php'>Video Elevators</a></li>
<li><a href='elv-el.php'>Elevator Lifts</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Controllers</a>
<ul class="parent">
<li><a href='ctl-wi.php'>Wireless</a></li>
<li><a href='ctl-dx.php'>DMX Controls</a></li>
<li><a href='ctl-dl.php'>DasLight</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">DJ Consoles</a>
<ul class="parent">
<li><a href='cnl-st.php'>Stands</a></li>
</ul></li></ul>

<ul class='level2'>
<li><a class="parent" href="#">Furniture</a>
<ul class="parent">
<li><a href='frn-tb.php'>Tables</a></li>
<li><a href='frn-ch.php'>Chairs</a></li>
<li><a href='frn-bs.php'>Barstools</a></li>
</ul></li></ul>

</ul></li></ul>

</ul></li></ul>


[/code]
Copy linkTweet thisAlerts:
@criterion9Sep 22.2009 — You could only append 'class="parent"' when the level is not an array. That would eliminate any extra class assignments.

As for how to use a recursive function you could use something like (untested and might contain syntax issues):
[code=php]
function appendMenu($arr,$level=0){
echo "<ul class="parent">";
foreach($arr as $item){
echo "<li>";
if(is_array($item)){
//you can insert the link here since this would be a "parent" item
appendMenu($item,$level++);
} else {
echo "your content here"; //regular items would go here
}
echo "</li>";
}
echo "</ul>";
}
[/code]

You would then just pass your array to the function once.
Copy linkTweet thisAlerts:
@MindzaiSep 22.2009 — I would agree that a recursive function is the way to go for outputting tree format data. Much more flexible than hard coding something.

PS You could do away with the $level argument in the above code since it isn't used for anything.
Copy linkTweet thisAlerts:
@criterion9Sep 22.2009 — I thought the OP was using the level to assign css classes so I left it in. Thanks for the catch though. :-)
Copy linkTweet thisAlerts:
@ehimeauthorSep 22.2009 — I was, I'm actually trying to get away from it altogether since it's causing nasty nesting issues,

but my JQuery sucks, it's the truth. BTW Criterion, I keep getting crazy formatting with your code.

The recursive function needs quite a bit of massaging, I'll try to play with it tomorrow, I guess I

have "Real Work" aka stuff you can see, to do for the rest of the day. Thanks guys for sticking

with this.
Copy linkTweet thisAlerts:
@criterion9Sep 22.2009 — What do you mean by crazy formatting? Do you have an example of the output and the function as you are using it?
×

Success!

Help @ehime spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...