• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hierarchical <ul><li> from MySql

#3
[eluser]zerone[/eluser]
This can - and should - be acomplished with one query.
The trick is to organize your items on an array.

Iterate all your menu entries and each one will go to a bag:

Code:
$all_items[ $row['parent_id'] ][] = $row;

Then just implement with your's friend function or create your own.
The function is first called with with the bag with empty parent_id;

Code:
echo recursive_tree($all_items['']);

Then recursive_tree is only responsible for printing each item of $items childs:

Code:
function recursive_tree($items)
{
    global $all_item;

    echo '<ul>';
    foreach ($items as $item)
    {
        echo "<li>{$item['name']}</li>";
        recursive_tree($all_items[ $item['id'] ]);
    }
    echo '</ul>';
}

There are prettier ways to do this, even by OOP which would be a lot more customizable.


Messages In This Thread
Hierarchical <ul><li> from MySql - by El Forum - 12-15-2007, 04:51 PM
Hierarchical <ul><li> from MySql - by El Forum - 12-16-2007, 02:25 AM
Hierarchical <ul><li> from MySql - by El Forum - 12-16-2007, 09:22 PM
Hierarchical <ul><li> from MySql - by El Forum - 12-16-2007, 09:40 PM
Hierarchical <ul><li> from MySql - by El Forum - 12-17-2007, 04:20 PM
Hierarchical <ul><li> from MySql - by El Forum - 12-18-2007, 06:00 AM
Hierarchical <ul><li> from MySql - by El Forum - 12-18-2007, 08:53 AM
Hierarchical <ul><li> from MySql - by El Forum - 12-18-2007, 09:04 AM
Hierarchical <ul><li> from MySql - by El Forum - 12-20-2007, 02:01 AM
Hierarchical <ul><li> from MySql - by El Forum - 10-25-2009, 01:49 PM
Hierarchical <ul><li> from MySql - by El Forum - 10-26-2009, 04:14 AM
Hierarchical <ul><li> from MySql - by El Forum - 08-04-2010, 09:06 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.