I have for my website a 4 level menu structure: sections (in sections tabel), categories (in categories tabel, with section_id pointing to sections) and then pages (in tabel pages I have parent_id because a page can be actually child of another page, and also each page belong to a section).
How can I build the menu? (I use PHP and MySQL) Now I use 4 nested queries. F开发者_C百科irst I select all sections, then for each section I select all categories, and for all categories I select pages without a parent, and then for each page I select child pages.
Also I can use just one query (with RIGHT JOIN) to get an array with section_id, category_id, page_id, parent_id. But I don't know how to convert this array to an tree array.
Use this class to convert your query to a tree array.
An example of use.
It's as simple as :
$sql_query = "SELECT field1, field2 FROM table WHERE field3='val1' AND field4=5 OR field5='val2';";
$query2tree = new dqml2tree($sql_query);
$sql_tree = $query2tree->make();
print_r($sql_tree);
精彩评论