Page 1 of 1

Variable in a variable name

Posted: Sun Mar 05, 2006 6:39 am
by phpgeek2006
Hi,
Does anyone know how to get a variable in a variable name?

Thanks,
Chris

Posted: Sun Mar 05, 2006 6:45 am
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

Posted: Sun Mar 05, 2006 6:54 am
by timvw

Posted: Sun Mar 05, 2006 6:56 am
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

Posted: Sun Mar 05, 2006 7:01 am
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()