Variable in a variable name

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
phpgeek2006
Forum Newbie
Posts: 2
Joined: Sun Mar 05, 2006 6:34 am

Variable in a variable name

Post by phpgeek2006 »

Hi,
Does anyone know how to get a variable in a variable name?

Thanks,
Chris
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You might want to look into arrays.

Code: Select all

$One = 1;
$Two = 2;

$Data = array();

$Data[$One] = "Whatever";
$Data[$Two] = $Anything;

echo $Data[1]; // prints Whatever
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

phpgeek2006
Forum Newbie
Posts: 2
Joined: Sun Mar 05, 2006 6:34 am

Post by phpgeek2006 »

Say i wanted to have a new variable everytime there was a record in the database, how would that be done?
(ie - 5 records were found = 5 new variables were created)
Thanks,
Chris
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Use a while loop:

Code: Select all

$Resource = mysql_query("select `field1`, `field2` from `table`",$Link_ID);

$Counter = 0;
$Records = array();
while ($Data = mysql_fetch_assoc($Resource)) {
  $Records[$Counter] = $Data;
  $Counter++;
}

// then you can get it back out

echo $Records[0]['field1'];
I'm not sure this is what you want though. There is probably an easier way to do what you are trying to accomplish. You will need to research the php array functions.

You could also use array_push()
Post Reply