[snip] > What I'm trying to do is list a typical category/subcategory system with > parents and children associated with those parents. My database table > (categories) lists all the parents and children together, each with a > parent_id field > (with root being a value of 0). What I want to do is, if a user clicked on > one of the parent categories, only one level of that category will show (or > only the direct children of that specific category will show). I want the > depth to be endless because I want to control the depth some other way. I > know that using a recursive theory would cost a lot as far as speed goes, > but I'm willing to risk it for now. > > Here is the problem: The problem is that the recursive method yields several > arrays, instead of one long array. I tried to use an array_push() function, > but that doesn't seem to work well with multi-dimentional arrays. I tried > the straight way, i.e. $menu_array[$count]['name'] = $name_of_category or > $menu_array['name'][$count] = $name_of_category, but that yields several > arrays instead of one long array or category names. The depth of the > categories can be determined by the $_GET string passed, $_GET['some_path'], > which is in the format: parent1_child1_grandchild1_grandchild2, > etc.($some_path = 1_4_6_8), where all of these are related to each other. > These, of course, are split using the underscore delimeter: $path['0'] = 1, > $path['1'] = 4, and so on. > > And finally, here is my question: How do I get all these categories, parents > and children, listed into one array and then returned. I want to be able to > list them on the web page using one array. I will also include id, > parent_id, and other info with each array, but first I want to get the name > listings of the categories to work. Also, if anyone has any suggestions > about a more speedier way to do this, please let me know.
I'm currently experimenting with something very similar to what you describe, and having success by creating one array from a database query, then using foreach loops to go through that array to display the main menu, find submenu items or create submenu arrays as needed. However, I'm limiting menus to three levels deep, so I can get away with conditional checks that only need to consider id, parent id, and grandparent id. Here's how I create the main array: $menu_sql = "SELECT id, parentid, menuname FROM pages WHERE displaystatus='Y' ORDER BY parentid, displayorder"; $menu_result = mysql_query($menu_sql); if (mysql_num_rows($menu_result) > 0) { while ($menu_row = mysql_fetch_array($menu_result)) { $menu_array[] = array($menu_row["id"], $menu_row["parentid"], $menu_row["menuname"]); } } Here's the code that creates the main menu: // home id is 10 $id = isset($_GET["id"]) ? $_GET["id"] : 10; $pid = isset($_GET["pid"]) ? $_GET["pid"] : 0; $gpid = isset($_GET["gpid"]) ? $_GET["gpid"] : ""; // create tab menu echo("<div id=\"tabs\">\n<ul>\n"); foreach ($menu_array as $menu) { if ($menu[1] == 0) { if ($menu[0] == $id || $menu[0] == $pid || $menu[0] == $gpid) { echo("<li id=\"selected\"><a href=\"" . $PHP_SELF . "?id=" . $menu[0] . "&pid=0\">" . $menu[2] . "</a></li>\n"); } else { echo("<li><a href=\"" . $PHP_SELF . "?id=" . $menu[0] . "&pid=0\">" . $menu[2] . "</a></li>\n"); } } } echo("</ul>\n</div>\n"); Second-level and third-level menus are created in a similar manner: if ($gpid == "0") { // display children of parent page, then children of current page // build submenu of child pages of parent page foreach ($menu_array as $subcheck) { if ($subcheck[1] == $pid) { $sublevel[] = array($subcheck[0], $subcheck[1], $subcheck[2]); } } if (count($sublevel) > 0) { $menu_count = 0; echo("<div id=\"submenu\">\n"); foreach ($sublevel as $sublink) { $menu_count++; if ($menu_count < count($sublevel)) { if ($sublink[0] == $id) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\" style=\"font-weight: bold; color: #900;\">" . $sublink[2] . "</a> | \n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\">" . $sublink[2] . "</a> | \n"); } } else { if ($sublink[0] == $id) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\" style=\"font-weight: bold; color: #900;\">" . $sublink[2] . "</a>\n</div>\n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\">" . $sublink[2] . "</a>\n</div>\n"); } } } } // build submenu of child pages of current page foreach ($menu_array as $subcheck2) { if ($subcheck2[1] == $id) { $sublevel2[] = array($subcheck2[0], $subcheck2[1], $subcheck2[2]); } } if (count($sublevel2) > 0) { $menu_count = 0; echo("<div id=\"submenu2\">\n"); foreach ($sublevel2 as $sublink2) { $menu_count++; if ($menu_count < count($sublevel2)) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $pid . "\">" . $sublink2[2] . "</a> | \n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $pid . "\">" . $sublink2[2] . "</a>\n</div>\n"); } } } } elseif ($gpid != "" && $gpid != "0") { // display children of grandparent page, then children of parent page // build submenu of children of grandparent page foreach ($menu_array as $subcheck) { if ($subcheck[1] == $gpid) { $sublevel[] = array($subcheck[0], $subcheck[1], $subcheck[2]); } } if (count($sublevel) > 0) { $menu_count = 0; echo("<div id=\"submenu\">\n"); foreach ($sublevel as $sublink) { $menu_count++; if ($menu_count < count($sublevel)) { if ($sublink[0] == $pid) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\" style=\"font-weight: bold; color: #900;\">" . $sublink[2] . "</a> | \n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\">" . $sublink[2] . "</a> | \n"); } } else { if ($sublink[0] == $pid) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\" style=\"font-weight: bold; color: #900;\">" . $sublink[2] . "</a>\n</div>\n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink[0] . "&pid=" . $sublink[1] . "&gpid=0\">" . $sublink[2] . "</a>\n</div>\n"); } } } } // build submenu of child pages of parent page foreach ($menu_array as $subcheck2) { if ($subcheck2[1] == $pid) { $sublevel2[] = array($subcheck2[0], $subcheck2[1], $subcheck2[2]); } } if (count($sublevel2) > 0) { $menu_count = 0; echo("<div id=\"submenu2\">\n"); foreach ($sublevel2 as $sublink2) { $menu_count++; if ($menu_count < count($sublevel2)) { if ($sublink2[0] == $id) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $gpid . "\" style=\"font-weight: bold; color: #900;\">" . $sublink2[2] . "</a> | \n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $gpid . "\">" . $sublink2[2] . "</a> | \n"); } } else { if ($sublink2[0] == $id) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $gpid . "\" style=\"font-weight: bold; color: #900;\">" . $sublink2[2] . "</a>\n</div>\n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $gpid . "\">" . $sublink2[2] . "</a>\n</div>\n"); } } } } } elseif ($pid == "0") { // only display children of current page // build submenu of child pages of current page foreach ($menu_array as $subcheck2) { if ($subcheck2[1] == $id) { $sublevel2[] = array($subcheck2[0], $subcheck2[1], $subcheck2[2]); } } if (count($sublevel2) > 0) { $menu_count = 0; echo("<div id=\"submenu\">\n"); foreach ($sublevel2 as $sublink2) { $menu_count++; if ($menu_count < count($sublevel2)) { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $pid . "\">" . $sublink2[2] . "</a> | \n"); } else { echo("<a href=\"" . $PHP_SELF . "?id=" . $sublink2[0] . "&pid=" . $sublink2[1] . "&gpid=" . $pid . "\">" . $sublink2[2] . "</a>\n</div>\n"); } } } } Sorry for all the code, and sorry I can't offer an example URL (don't think the client would like me to at this stage), but HTH. -- Lowell Allen -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php