naming variables [SOLVED]

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

naming variables [SOLVED]

Post 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?
Last edited by s.dot on Thu Oct 27, 2005 10:47 am, edited 1 time in total.
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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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'];
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

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

Post 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. :)
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.
Post Reply