Problem Creating Dynamic Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Toot4fun
Forum Commoner
Posts: 25
Joined: Wed Dec 10, 2003 11:44 am

Problem Creating Dynamic Variables

Post by Toot4fun »

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.

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] . "'";
}
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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

foreach($colors as $color){
  mysql_query("SELECT * FROM colortbl WHERE color='$color'");
}
:?:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there a reason you want to have all these queries separate? It seems like they'd work much better together, performance-wise it definitely will anyways.
Toot4fun
Forum Commoner
Posts: 25
Joined: Wed Dec 10, 2003 11:44 am

Post by Toot4fun »

That is a very valid question and that's my fault because my post was not correct. It should be like this:

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 tbl" . $colors[$i];
}
Each color is a different table, so I'm trying to just run all the queries at once.

$queryBlack = "SELECT * FROM tblBlack";
$queryBlue = "SELECT * FROM tblBlue";

I apologize for the confusion.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

$colors = array();
$colors[] = "Black";
$colors[] = "Blue";
$colors[] = "Silver";
$colors[] = "Tan";
$colors[] = "Gold";
$colors[] = "White";
$colors[] = "Red";

foreach($colors as $color)
{
  ${'query' . $color} = 'SELECT * FROM `tbl' . $color . '`';
}
For how it works, read Variable Variables.
Toot4fun
Forum Commoner
Posts: 25
Joined: Wed Dec 10, 2003 11:44 am

Post by Toot4fun »

PERFECT! I had no idea that even existed. Thank you very much for the help.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

why not use a join?
Post Reply