View Single Post
Old 11-18-2009, 01:25 PM   #1 (permalink)
maZtah
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default Recursive arrays

Hi guys.

I have a MySQL table 'categories'. Every category has a 'parent_id' field. All the main elements have parent_id=0.

What I want is to make an multidimensional array which I can loop through to put in an unordened list (<ul>).

Something like:

PHP Code:
<ul>
<?php
foreach($aCategories as $aRow)
{ ?>
    <li>
        <a href="#"><?php echo $aRow['name']; ?></a>
<?php
    $aChild = $aRow['child'];
    if (is_array($aChild))
    { ?>
        <ul>
<?php
        foreach($aChild as $aChildRow)
        { ?>
            <li><a href="#"><?php echo $aChildRow['name'];</a>
<?php
        } ?>
        </ul>
<?php
    } ?>
    </li>
<?php
} ?>
</ul>

I hope you understand what I'm trying to achieve.

Thanks for your help.
maZtah is offline  
Reply With Quote