Problem with getting desired results using loops and queries

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
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Problem with getting desired results using loops and queries

Post by davidprogramer »

The syntax is all correct, so try to focus on logic here. My goal is to loop through all of the league ladders (found on .$prefix."_league_ladders) and then loop through each game database (using the ladder id we get from league ladders) and then loop through the _.$sql_prefix."_clans table to get the clans. If the player isn't in a clan on the current ladder being looped then don't list anything. (In other words, only list clans that the player is in from each game ladder). I know there has to be an easier way of doing this. Any help?

Code: Select all

$array_clans = array();

$sql_ladders = sql_query("SELECT ladder_id FROM ".$prefix."_league_ladders");
$sql_clans = mysql_query("SELECT clan_id FROM ".$sql_prefix."_clans WHERE clan_id = $clanid");

echo "<table>
	<form name=\"form1\" method=\"post\">
		Ladder: &nbsp<select name=\"menuClanHQ\">";
		for ($m=1; $m < sql_num_rows($sql_ladders)+1; $m++){
			list($ladder_id) = sql_fetch_row($sql_ladders);
			for ($n=1; $n < sql_num_rows($sql_clans)+1; $n++){
				list($clan_id) = sql_fetch_row($sql_clans);
				if ($clan_id <> NULL){
					echo "<option value=\"modules.php?name=League&file=clan_hq&lid=$m&cid=$clan_id\">$clan_id</option>";
				}
			}
		}			
		echo "</select>&nbsp
		<input type=\"button\" name=\"Button1\" value=\"Go\" onClick=\"MM_jumpMenuGo('menuClanHQ','parent',0)\">
	</form>
</table></center><br>";
This code gets the correct clan of ladder1, but doesn't loop to ladder2 or 3.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

What are sql_query(), sql_num_rows() and sql_fetch_row()? Should you be using the mysql_*() functions throughout?
(#10850)
davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

I am designing this inside a phpNuke module. phpNuke has different functions for those to ease the process of writing code.

sql_fetch_row is the same as $db->sql_fetch_row()

Same with the others. The only time I use regular php or mysql functions is when I am using a custom database that doesn't use phpNuke. (In this case, the game databases). I believe my only problem is somewhere in the for loops. I am not quite sure, but for some reason it isn't looping (I think).

edit:

The following code checks all 3 ladders but returns the wrong clan and ID. It also adds options for clans that dont exist. For instance. If I am in a clan on ladder 1 and 2 but I am not in one on ladder 3, it gives the clan id found on ladder 1 to the other options as well. So if my clan id for ladder 1 is 12, it will say my clan id for ladder 2 and 3 is 12 also. Which is wrong. (ladder 3 shouldnt be there and clan id should be different numbers). Where did I go wrong?

Code: Select all

echo "<table>
	<form name=\"form1\" method=\"post\">
		Ladder: &nbsp<select name=\"menuClanHQ\">";
		for ($m=1; $m < sql_num_rows($sql_ladders)+1; $m++){
			list($ladder_id) = sql_fetch_row($sql_ladders);
			$sql_clans = mysql_query("SELECT clan_id FROM ".$sql_prefix."_clans WHERE clan_id = $clanid");
			for ($n=1; $n < sql_num_rows($sql_clans)+1; $n++){
				list($clan_id) = sql_fetch_row($sql_clans);
				if ($clan_id <> NULL){
					echo "<option value=\"modules.php?name=League&file=clan_hq&lid=$ladder_id&cid=$clan_id\">$clan_id</option>";
				}
			}
		}			
		echo "</select>&nbsp
		<input type=\"button\" name=\"Button1\" value=\"Go\" onClick=\"MM_jumpMenuGo('menuClanHQ','parent',0)\">
	</form>
</table></center><br>";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

davidprogramer
Forum Commoner
Posts: 64
Joined: Mon Nov 28, 2005 6:11 pm

Post by davidprogramer »

Thanks :-D (I was hoping I'd never need join but might as well learn it)
Post Reply