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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

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

Post 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
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

The problem with using joins is I couldnt then while through the sub joins?
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

anyone? its taking 12queries, thats too much
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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`";
tony montana
Forum Newbie
Posts: 21
Joined: Tue Sep 28, 2004 11:10 am

Post by tony montana »

how would I do seperate whiles?
Post Reply