Jump to content

syntax for adding variable name inside another variable name


chiefrokka

Recommended Posts

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.

 

 

Code:

 

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

Your code is all over the place... here, I figured re-writing it for you is better aid than anything really.

 

<?php
$mWinner[1] = $_POST['Matchup_01_Winner']; // returns Team1 or Team2
$mWinner[2] = $_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(!empty($mWinner[$i]))
{
	echo '<br>matchup ' .$i. ' Winner = '.$mWinner[$i];

	//Insert matchup winner into the database table before moving to next matchup
	mysql_query("UPDATE `Teams` SET `Matchup_".$i."_Winner` = '" .$mWinner[$i]. "' Where `NFL` = 'Matchup_Winners'") or die(mysql_error());

}
} // end of for loop, all 16 matchups	
?>

Link to comment
Share on other sites

What error are you actually getting?  The only problem I can see is that $i will have the value of 1, 2 etc rather than 01, 02, which won't match up with your column names.

 

Let us know what error you're getting from mysql_error()

 

oh ya, sorry.  I went and changed the tables names and got rid of the 01, 02 so it's just 1, 2 to match $i

 

Parse error: parse error, unexpected '\"' in /home/content/c/h/i/chiefrokka/html/survivor/Update_Winners.php on line 127

 

which is the if(trim($Matchup   

Link to comment
Share on other sites

Your code is all over the place... here, I figured re-writing it for you is better aid than anything really.

 

<?php
$mWinner[1] = $_POST['Matchup_01_Winner']; // returns Team1 or Team2
$mWinner[2] = $_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(!empty($mWinner[$i]))
{
	echo '<br>matchup ' .$i. ' Winner = '.$mWinner[$i];

	//Insert matchup winner into the database table before moving to next matchup
	mysql_query("UPDATE `Teams` SET `Matchup_".$i."_Winner` = '" .$mWinner[$i]. "' Where `NFL` = 'Matchup_Winners'") or die(mysql_error());

}
} // end of for loop, all 16 matchups	
?>

 

thanks a bunch.  i should've used arrays before not sure why i forgot to.  a lot cleaner. 

 

everything seems to work fine with your code, except only thing you missed is grabbing team name from database.  your code returns the correct values "Team1", "Team2", etc.

 

I'm trying this but doesn't seem to be working after i grab the team name from database 

$MatchupWinner[$i] = $row['$MatchupWinner[$i]']; 

Link to comment
Share on other sites

What error are you actually getting?  The only problem I can see is that $i will have the value of 1, 2 etc rather than 01, 02, which won't match up with your column names.

 

Let us know what error you're getting from mysql_error()

 

oh ya, sorry.  I went and changed the tables names and got rid of the 01, 02 so it's just 1, 2 to match $i

 

Parse error: parse error, unexpected '\"' in /home/content/c/h/i/chiefrokka/html/survivor/Update_Winners.php on line 127

 

which is the if(trim($Matchup   

 

even though Jeremy's way is easier and I'll use his, I'd still would like to know how to write the syntax to include $i into the middle of another variable name... just in case I need it later on.  please let me know what's wrong with this code still. 

 

<?php
$Matchup_1_Winner = $_POST['Matchup_1_Winner']; // returns Team1 or Team2
$Matchup_2_Winner = $_POST['Matchup_2_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) != "") // this is line 127 for error i was getting.  prob. not the only error
{
	$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.