View Single Post
Old 08-25-2008, 01:26 PM   #6 (permalink)
maZtah
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

That's nice!

;)

First select all the categories:

PHP Code:
$szQuery "SELECT id, parentId, name FROM categories ORDER BY parentId ASC, name ASC";
$pSidebar mysql_query($szQuery) or die(mysql_error()); 
Then put the results into an array:

PHP Code:
if (mysql_numrows($pSidebar))
{
    while (
$aRow mysql_fetch_array($pSidebar))
    {
        if (
$aRow['parentId'] == 0) {
            
$aParent[$aRow['id']] = $aRow;
        }
        else
        {
            
$aParent[$aRow['parentId']]['child'][] = $aRow;
        }
    }

Then output!

PHP Code:
<ul id="categories">
<?php
foreach ($aParent as $iParent => $aData)
    { 
?>
            <li>
                <?php echo $aData['name']; ?>
<?php
        $aChild 
$aData['child'];
        if (
is_array($aChild))
        { 
?>
                <ul>
<?php
            
foreach ($aChild as $iChild => $aSubData)
            { 
?>
                    <li>
                        <?php echo $aSubData['name']; ?>
                    </li>
<?php
            
?>
                </ul>
<?php
        
?>
</ul>
Good luck
maZtah is offline  
Reply With Quote