Jump to content

parsing table fields


chiefrokka

Recommended Posts

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

 

 

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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	
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.