Page 1 of 1

Help with nested loops please

Posted: Wed Apr 28, 2010 11:41 am
by kybaker
Hello All,

I am having troubles getting my code to work properly. Basically what I have is a page where people can bet on games, if they bet on the game I would like the game (which is a table) to disappear. Since there will be multiple users betting I need to make sure that it checks whether the person currently logged in has made a bet on the game or not.

so here is my thought process:

if (game has not been bet on){
display game
}
else {
if ( game has been bet on and the member_id that bet is != to the current user){
continue;
}
else {
display game
}
}

------------------------------------
Here is my code for that section, any suggestions would be appreciated. The website is bigtenbetting.com if anyone wants to look.

Code: Select all

         while($gameinfo = mysql_fetch_array( $data )){
		//if there is no bet on game; display game
		if(is_null($gameinfo['sport_event'])){
			echo "<form id=\"betForm\" name=\"betForm\" method=post action=\"bet-exec.php\">";
					$sport_event_id = $gameinfo['sport_event_id'];
					$homeID = $gameinfo['home_id'];
					$awayID = $gameinfo['away_id'];
					$data2 = mysql_query("SELECT school_name, team_id FROM teams WHERE team_id='$homeID'")
						or die(mysql_error());
					$data3 = mysql_query("SELECT school_name, team_id FROM teams WHERE team_id='$awayID'")
						or die(mysql_error());
					$homeschool = mysql_fetch_array( $data2 );
					$awayschool = mysql_fetch_array( $data3 );
					$home = $homeschool['school_name'];
					$away = $awayschool['school_name'];
					
					echo "<table align=\"center\">
						<thead>
							<tr> 
								<th width=\"167\" scope=\"col\">".$gameinfo['game_date']."</th> 
								<th width=\"94\" scope=\"col\">".ucwords($gameinfo['sport'])."</th> 
			 
								<th width=\"79\" scope=\"col\">Team ID</th> 
								<th width=\"216\" scope=\"col\">Teams</th> 
								<th width=\"70\" scope=\"col\">Selection</th> 
							</tr> 
						</thead>
						<tfoot> 
							<tr> 
								<th scope=\"row\"><input type=submit name=submit_bet value=\"Place Bet\" /></th>
								<td colspan=\"4\"><input name=\"bet_amt\" type=text class=textfield id=\"bet_amt\" /></td>	
								<td><input name=sport_event_id type=hidden value=$sport_event_id /></td>
							</tr> 
						</tfoot> 
						<tbody> 
							<tr > 
								<th scope=\"row\" id=\"r101\"></th> 
								<td></td> 
			 
								<td>".$homeID."</td> 
								<td><strong>".$homeschool['school_name']."</strong></td> 
								<td><input name=\"team_radio\" type=radio value=$home /></td> 
							</tr> 
								<tr class=\"odd\"> 
								<th scope=\"row\" id=\"r100\"></th> 
			 
								<td></td> 
								<td>".$awayID."</td> 
								<td><strong>".$awayschool['school_name']."</strong></td> 
								<td><input name=team_radio type=radio value=$away /></td> 
							</tr> 
						</tbody>		
					</table>
					</form>
					<br />
					<br />";
			}
			//if bet on game
			else {
				//if bet is from current member
				if($gameinfo['member_id'] == $_SESSION['SESS_MEMBER_ID']){
					continue 2;
				}
				else {
					echo "<form id=\"betForm\" name=\"betForm\" method=post action=\"bet-exec.php\">";
					$sport_event_id = $gameinfo['sport_event_id'];
					$homeID = $gameinfo['home_id'];
					$awayID = $gameinfo['away_id'];
					$data2 = mysql_query("SELECT school_name, team_id FROM teams WHERE team_id='$homeID'")
						or die(mysql_error());
					$data3 = mysql_query("SELECT school_name, team_id FROM teams WHERE team_id='$awayID'")
						or die(mysql_error());
					$homeschool = mysql_fetch_array( $data2 );
					$awayschool = mysql_fetch_array( $data3 );
					$home = $homeschool['school_name'];
					$away = $awayschool['school_name'];
					
					echo "<table align=\"center\">
						<thead>
							<tr> 
								<th width=\"167\" scope=\"col\">".$gameinfo['game_date']."</th> 
								<th width=\"94\" scope=\"col\">".ucwords($gameinfo['sport'])."</th> 
			 
								<th width=\"79\" scope=\"col\">Team ID</th> 
								<th width=\"216\" scope=\"col\">Teams</th> 
								<th width=\"70\" scope=\"col\">Selection</th> 
							</tr> 
						</thead>
						<tfoot> 
							<tr> 
								<th scope=\"row\"><input type=submit name=submit_bet value=\"Place Bet\" /></th>
								<td colspan=\"4\"><input name=\"bet_amt\" type=text class=textfield id=\"bet_amt\" /></td>	
								<td><input name=sport_event_id type=hidden value=$sport_event_id /></td>
							</tr> 
						</tfoot> 
						<tbody> 
							<tr > 
								<th scope=\"row\" id=\"r101\"></th> 
								<td></td> 
			 
								<td>".$homeID."</td> 
								<td><strong>".$homeschool['school_name']."</strong></td> 
								<td><input name=\"team_radio\" type=radio value=$home /></td> 
							</tr> 
								<tr class=\"odd\"> 
								<th scope=\"row\" id=\"r100\"></th> 
			 
								<td></td> 
								<td>".$awayID."</td> 
								<td><strong>".$awayschool['school_name']."</strong></td> 
								<td><input name=team_radio type=radio value=$away /></td> 
							</tr> 
						</tbody>		
					</table>
					</form>
					<br />
					<br />";
				}
			}
	}
Thanks for any help, this is due Friday for a project and I'm kind confused why it isn't working :banghead: :banghead: :banghead:

Re: Help with nested loops please

Posted: Wed Apr 28, 2010 2:36 pm
by tr0gd0rr
I only see one loop--the while loop at the top. If so, you should just need `continue;` instead of `continue 2;`. You could also simplify your logic a bit by using `elseif`.

Re: Help with nested loops please

Posted: Wed Apr 28, 2010 5:15 pm
by kybaker
Hello,

I actually did change that right after I submitted my post. I'm still a pretty big beginner PHP coder so I'm still learning a lot.
I am not sure if my code is even not correct. It works for one user but when I try to make it work for all users it doesn't.
I actually am starting to think it might be how I join my databases in my mysql query.

If anyone has any other options for question let me know.

Thanks.