Page 1 of 1

Three queries - whiled within each other (Cut them down?)

Posted: Mon Oct 25, 2004 6:29 pm
by tony montana
Im wondering if theres some other method you guys can show me to accomplish this code with less queries.

Im aware its set out weird or unreable (im sorry, its just my style) so it might look quite alot, but its actually quite simple.

Code: Select all

function forum_dropdown($selected, $altLocations)	
	{global $forumzDB, $user_properties;
	
	$query_categories = query("SELECT cat_title, cid FROM $forumzDB.forumz_categories ORDER BY `order`") or die_query("fuction_forum_dropdown_1");
	while($categ = mysql_fetch_object($query_categories)) {
	$forum_output = '';
						
						$query_forums = query("SELECT forum_title, fid FROM $forumzDB.forumz_forums
						WHERE level <= ".$user_properties['status_id']." 
						AND type != 'lock' AND subFID = '' AND cid = '$categ->cid'
						ORDER BY `order`") or die_query("fuction_forum_dropdown_2");
						while($forum = mysql_fetch_object($query_forums)) {
						$selected_html = '';
						
						if($selected == $forum->fid)
						{$selected_html = ' selected = "selected"';}
						$forum_output .= "<option value="".$forum_properties['base_url']."forum/$forum->fid"$selected_html> -- $forum->forum_title</option>";
					
									$query_subs = query("SELECT forum_title, fid FROM $forumzDB.forumz_forums
									WHERE level <= ".$user_properties['status_id']." 
									AND type != 'lock' AND  subFID = '$forum->fid'  
									ORDER BY `order`") or die_query("fuction_forum_dropdown_3");
									while($subs = mysql_fetch_object($query_subs)) {
									$selected_html = '';
									
									if($selected == $forum->fid)
									{$selected_html = ' selected = "selected"';}
									$forum_output .= "<option value="".$forum_properties['base_url']."forum/$subs->fid"$selected_html> ---- $subs->forum_title</option>";
									}#end sub forums
						}#end forum

						if($forum_output)
						{$output = $output.'<option></option>
											<option>Category : '.$categ->cat_title.'</option>'.$forum_output;}

}#end categ

	if($altLocations == 1)
	{$alt_locations = "<option value="".$forum_properties['base_url']."">Home</option>
			<option value="".$forum_properties['base_url']."active_topics">Active Topics</option>
			<option value="".$forum_properties['base_url']."online_users">Online Users</option>
			<option value="".$forum_properties['base_url']."memberslist">Memberslist</option>
			<option value="".$forum_properties['base_url']."edit_account">Control Panel</option>";}

	return '<form name="dropdownForm"><select onchange="window.location=document.dropdownForm.dropdownOptions.options[document.dropdownForm.dropdownOptions.selectedIndex].value" name="dropdownOptions" class="textfield">
	'.$alt_locations.$output.'</select></form>';}
	
	echo forum_dropdown(16, 1);
A working example can be seen here; http://forumz.wuggawoo.co.uk/index/test

Posted: Mon Oct 25, 2004 6:41 pm
by kettle_drum
You can do a query like:

Code: Select all

SELECT table.field, table2.field, table2.field2 FROM table, table2.........
You code looks fine to me though - nice and easy to read and understand.

Posted: Mon Oct 25, 2004 7:04 pm
by tony montana
The problem with using joins is I couldnt then while through the sub joins?

Posted: Tue Oct 26, 2004 5:27 pm
by tony montana
anyone? its taking 12queries, thats too much

Posted: Tue Oct 26, 2004 7:26 pm
by Christopher
Just a stab, but sometthing like:

Code: Select all

$ct = "$forumzDB.forumz_categories";
$ft = "$forumzDB.forumz_forums
$sql = "SELECT $ft.cat_title, ....
FROM $ft LEFT JOIN $ct ON $ft.cid = $ct.id
WHERE level <= ".$user_properties['status_id']." 
  AND $ft.type != '$ft.lock' 
  AND `$ft.subFID = '' 
ORDER BY `$ft.cid`",`$ft.order`";

Posted: Wed Oct 27, 2004 4:45 am
by tony montana
how would I do seperate whiles?