chiefrokka Posted February 21, 2008 Share Posted February 21, 2008 I have a table called Users. In that table I have all the fields like "Name", "Password", "Week1_Pick", "Week2_Pick", "Week3_Pick", etc. how would I go about creating a nice for loop to parse all the Week1_Pick, Week2_Pick values? something like this if ($Pick == $row['Week1_Pick']) but I need to compare all those fields for that user... figured there's an easier way than tons of if loops Quote Link to comment Share on other sites More sharing options...
bpops Posted February 21, 2008 Share Posted February 21, 2008 <?php $query = "SELECT Week1_Pick, Week2_Pick FROM Users"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ if($pick == $row['Week1_Pick']){ // do something } if($pick == $row['Week2_Pick']){ // do something else } } ?> That doesn't look like too many if loops to me. Quote Link to comment Share on other sites More sharing options...
bpops Posted February 21, 2008 Share Posted February 21, 2008 I just realized what you meant.. writing out new code... Quote Link to comment Share on other sites More sharing options...
bpops Posted February 21, 2008 Share Posted February 21, 2008 <?php $query = "SELECT * FROM Users"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $num_weeks = XX // <------ for($i=1; $i <= $num_weeks; $i++){ if($pick == $row['Week'.$i.'_Pick']){ echo 'picked '.$i; } } } ?> Quote Link to comment Share on other sites More sharing options...
chiefrokka Posted February 21, 2008 Author Share Posted February 21, 2008 <?php $query = "SELECT * FROM Users"; $result = mysql_query($query); while($row = mysql_fetch_array($result)){ $num_weeks = XX // <------ for($i=1; $i <= $num_weeks; $i++){ if($pick == $row['Week'.$i.'_Pick']){ echo 'picked '.$i; } } } ?> ok cool. i'll check it out. I didn't know you could break it up like $row['Week'.$i.'_Pick'] very good to know. thanks Quote Link to comment Share on other sites More sharing options...
chiefrokka Posted February 22, 2008 Author Share Posted February 22, 2008 what's the syntax to do these type of statements? I keep trying and can't figure it out. I get confused when to use ' and when to use "" I have tables named "Matchup_1_Winner", "Matchup_2_Winner", etc. I want to use a for loop to parse thu all matchups instead of doing 16 repetitive switch cases for each matchup. Here's my little for loop I'm getting errors on and keep getting confused what syntax to use to add $i into the middle of another variable. <?php $Matchup_01_Winner = $_POST['Matchup_01_Winner']; // returns Team1 or Team2 $Matchup_02_Winner = $_POST['Matchup_02_Winner']; // returns Team3 or Team4 // etc. for ($i=1; $i <= 16; $i++) // loop thru all 16 matchups { // check to see which matchups winners the Admin wants to update if(trim($Matchup_".$i."_Winner) != "") { $Matchup_'.$i.'_Winner = $row['Matchup_'.$i.'_Winner']; // grab Team Name from fields called "Team1", "Team2", etc. echo "<br>matchup 1 Winner = $Matchup_'.$i.'_Winner "; //Insert matchup winner into the database table before moving to next matchup mysql_query("UPDATE `Teams` SET `Matchup_".$i."_Winner` = '$Matchup_".$i."_Winner' Where `NFL` = 'Matchup_Winners'") or die(mysql_error()); } } // end of for loop, all 16 matchups ?> Quote Link to comment 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.