Page 1 of 1

How do I use a string's contents as a variable name?

Posted: Wed Jun 15, 2005 9:57 am
by aucyris
Here is a small portion of the code I have. I have cut out all of the in-between code and most html formatting provided by the code to get to the meat of the issue:

A form is created dynamically using the fields available from a table. I'll assume one field to eliminate the for loop.

Code: Select all

$fields = mysql_list_fields($dbname, $table, $link);

$i = 0;
$field_arrayї$i] = mysql_field_name($fields, $i);
print &quote;<input type=text name=$field_array&#1111;$i]>&quote;;
//Let's say it generates a text field with the name=ID.
When the form is submitted a value is assigned to $ID. How do I display this value using an echo statement?

Code: Select all

echo $ID;
//This works, but I won't always know the 
//name of the form field since it's created dynamically

$form_array&#1111;$i] = &quote;$&quote;. $field_array&#1111;$i];
$str = $form_array&#1111;$i];
echo $form_array&#1111;$i];
echo $str;
//This outputs &quote;$ID$ID&quote; instead of the value

$str2 = $field_array&#1111;$i];
echo $field_array&#1111;$i];
echo $str2;
//This outputs &quote;IDID&quote; instead of the value.

Posted: Wed Jun 15, 2005 10:28 am
by timvw
Read the manual... Chapter on variables... Section variable variables...
Search this forum for variable variables...

Code: Select all

$name = "timvw";
${$name} = "RTFM";
echo $timvw;