I have a table having hierarchical menus like
"id" "parent_id" "name"
1 0 menu
2 1 item1
3 2 item1_1
4 1 item2
5 4 item2_1
...
...
and I have 100s of menu items here. In order to get all items in an array I have to write a recursive function like this
getmenu function(parent_id = 1)
{
$items = mysql_query("SELECT id FROM table WHERE parent_id = " + parent_id);
while ($item = msyql_Fetch_assoc($items)) {
...here I put them in array and call recursive function again to get sub items...
getmenu($item['id']);
}
}
but this executes 100s of queries. Is this the best way to do this, to get hierarchical menus from database? Does this way loads mysql much?
- format and dump them in static php files every 5 minutes. Then I just include them and apply css. Since it's not that time critical 5 minutes is enough, even 1 minute is fine too. Server load dropped from 80% to 4% . Nice :)
– Ergec Jun 25 '10 at 07:26