Page 1 of 1

naming variables [SOLVED]

Posted: Thu Oct 27, 2005 10:12 am
by s.dot
I have a form where I need the value to be a $_POST variable

consider I have the following post variables

Code: Select all

$result_1_display = $_POST['result1'];
$result_2_display = $_POST['result2'];
$result_3_display = $_POST['result3'];
Now I have a text input field that need to hold the value of $result_#_display where the # sign is based on an array key... like this:

Code: Select all

foreach($array AS $k => $v)
{
   echo <input type=\"text\" name=\"name\" value=\"$result_$k_display\">";
}
Obviously, the value=\"$result_$k_display\"
I need to replace $k with the the value of $k... how do I do that?

Posted: Thu Oct 27, 2005 10:14 am
by Chris Corbyn
You could always send the form data as an array but anyway....

Use this syntax...

Code: Select all

${'result_'.$k.'_display'} = $_POST['result1'];

Posted: Thu Oct 27, 2005 10:16 am
by John Cartwright
Why not just do something along the lines of

Code: Select all

$results = array(
   $_POST['result1'], $_POST['result2'], $_POST['result3']
);

foreach ($result as $name) {
   echo '<input type="text\" name="name" value="'.$name.'">'; 
}

Posted: Thu Oct 27, 2005 10:47 am
by s.dot
Well, I would've done that with the post array, but the post array wasn't the array I was doing the foreach on... i just showed a generic example of what I was doing so I could try to explain my problem as clear as possible.

d11's post worked great.

Code: Select all

<?
echo "<input type=\"text\" name=\"name\" value=\"${'quiz_'.$k.'_result'}\">";
?>
Thanks guys. :)