Problem Creating Dynamic Variables
Posted: Wed Feb 22, 2006 11:06 am
I'm trying to create variables on the fly based on values in an array. However, I'm having some problems with the concatenation and I'm assuming that it's because I'm trying to join two variables together to create a new variable name.
What I'd like to have is this:
$queryBlack = "SELECT * FROM tblColors WHERE color='Black'";
$queryBlue = "SELECT * FROM tblColors WHERE color='Blue'";
etc...
Is this possible? If so, how is it done, because obviously I've missed something.
Code: Select all
$colors[] = "Black";
$colors[] = "Blue";
$colors[] = "Silver";
$colors[] = "Tan";
$colors[] = "Gold";
$colors[] = "White";
$colors[] = "Red";
for($i=0; $i <= (count($colors) - 1); $i++){
"$query" . $colors[$i] = "SELECT * FROM tblColors WHERE color='" . $colors[$i] . "'";
}$queryBlack = "SELECT * FROM tblColors WHERE color='Black'";
$queryBlue = "SELECT * FROM tblColors WHERE color='Blue'";
etc...
Is this possible? If so, how is it done, because obviously I've missed something.