spyke01 Posted July 6, 2006 Share Posted July 6, 2006 Hi guys, ive been dreading doing this for awhile, but finally got to it. I need to make a drop down box similar to IPB's "move topic to this forum". It will contain the category name using the optgroup tag, then all forums, and under each forum will be their subforums, if any. I figured to save processing time, i should do 1 database query, drop the results into an array, ksort it, and then print out the sectiosn.[u][b]Result should look like this:[/b][/u][b]Category[/b]-Forum--Sub Forum--Sub Forum[b]Category[/b]-Forum[u][b]Variables:[/b][/u]$categories[$cat_order] = array('catid' => '$cat_id', 'catname' => '$cat_name');$categories[$forum_cat_id][$forum_order] = array('forumid' => '$forum_id', 'subforumid' => '$forum_subforum_id', 'forumname' => '$forum_name');[u][b]Info:[/b][/u]The value of $forum_subforum_id is the same as the $forum_id that it belongs to.[u][b]Code so far:[/b][/u] (I have no idea if this will work, its a mock-up)[code]<? $categories = array(); $forums = array(); // Pull forumss from DB $sql = "SELECT * FROM forums"; $result = mysql_query($sql); if (mysql_num_rows($result) == 0) { echo "no results"; } else { while ($row = mysql_fetch_array($result)) { extract($row); $forums[$forums_cat_id][$forums_order] = array('forumid' => '$forums_id', 'forumsubforumid' => '$forums_subforum_id', 'forumname' => '$forums_name'); } } mysql_free_result($result); // Pull categories from DB $sql = "SELECT * FROM categories"; $result = mysql_query($sql); if (mysql_num_rows($result) == 0) { echo "no results"; } else { while ($row = mysql_fetch_array($result)) { extract($row); $categories[$cat_order] = $cat_name; ksort($forums[$cat_id]); // Sort forums for this category by order } ksort($categories); // Sort categories by order } mysql_free_result($result); ?>[/code]Can someone help me create this? I'm really unsure as to how to get this to work right with assigning variables as arrays, i try not to use them at all Quote Link to comment https://forums.phpfreaks.com/topic/13898-help-with-arrays/ Share on other sites More sharing options...
Barand Posted July 6, 2006 Share Posted July 6, 2006 Assuming you data structure is[pre]categories forum---------- -----------cat_id <--------- cat_idcat_name +--> forum_id | forum_name +--- forum_subforum_id[/pre]Try[code]<?phpinclude '../test/db.php';echo "<SELECT name='forum'>\n";$res1 = mysql_query("SELECT category_id, name from categories ORDER BY name") or die(mysql_error());while (list($cid, $cname) = mysql_fetch_row($res1)) { echo "<OPTGROUP label='$cname'>\n"; echo forumOptions($cid); echo "</OPTGROUP>\n";}function forumOptions($catid, $parent=0, $level=0) { $sql = "SELECT forum_id, forum_name FROM forums WHERE forum_subforum_id = $parent AND category_id = $catid"; $res = mysql_query($sql) or die(mysql_error()); $str = ''; while (list ($fid, $fname) = mysql_fetch_row($res)) { $indent = str_repeat('- ', $level+1); $str .= "<OPTION value='$fid'>$indent $fname</option>\n"; $str .= forumOptions($catid, $fid, $level+1); } return $str;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13898-help-with-arrays/#findComment-54156 Share on other sites More sharing options...
spyke01 Posted July 7, 2006 Author Share Posted July 7, 2006 Thanks a ton, it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/13898-help-with-arrays/#findComment-54413 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.