TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-18-2009, 01:25 PM   #1 (permalink)
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
Old 11-18-2009, 03:18 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

the code looks like it does the job. But if you don't know how many levels of children there are I'll suggest to use a recursive function.

php Code:
<?php
function print_categories($aCat) {
    echo "<ul>\n";
    echo "\t<li>";
    if(is_array($row)) {
        print_categories($row);
    } else {
        echo '<a href="#">'.$aRow['name'].'</a>';
    }
    echo "\t</li>";
    echo "</ul>\n";
}

print_categories($aCategories);
?>

Although I haven't tested the this code, the idea is to make it recursive when the input is an array.
tony is offline  
Reply With Quote
Old 11-19-2009, 08:13 AM   #3 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default

Thanks tony. But; how would I make the multidimensional array from the mysql results?

I did something like this, but this works only for 2 levels.

PHP Code:
public function sort_menu($aResult)
{
    foreach($aResult as $aRow)
    {         
        $aIds[] = $aRow['id'];
       
        if (!in_array($aRow['parent_id'], $aIds))
        {
            $aCategories[$aRow['id']] = $aRow;
        }
        else
        {
            $aCategories[$aRow['parent_id']]['child'][] = $aRow;
        }
    }
   
    return $aCategories;
}

Last edited by maZtah : 11-19-2009 at 09:34 AM.
maZtah is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
lol, i made a never ending loop with arrays sarmenhb Tips & Tricks 28 10-22-2012 09:36 AM
config file arrays oop memcache MJ_ General 2 01-24-2009 06:53 PM
2-d arrays -- issues of printing and deleting Dave Absolute Beginners 3 06-05-2008 11:45 AM
Arrays + Pages blayne4k Script Giveaway 0 04-20-2008 10:35 PM


All times are GMT. The time now is 04:16 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design