Page 1 of 1

unable to echo a dropdown box with php variables help!

Posted: Thu Dec 14, 2006 6:24 pm
by Mythic Fr0st
Ok, im trying to echo the contents of the array '$inv' into a dropdown box... (an inventory system)

however it only shows $inv[$key] in the dropdown box, how can I get it to recognize the $inv variable?

code:

Code: Select all

?>
<form method="post" action="HomePIG.php">
<select name='inv'>
<php
foreach ($inv as $key => $shw)
{
echo '<option value=$inv[$key]>$inv$[key]</option>';
}
?>
</select>
</form>
thats currently what I have, can anyone help?

Posted: Thu Dec 14, 2006 6:33 pm
by RobertGonzalez
Read the manual on strings (particularly the part about the types of quotes and what they do).

Re: unable to echo a dropdown box with php variables help!

Posted: Thu Dec 14, 2006 6:34 pm
by Begby
Try it with double quotes like this, when its single quotes it doesn't evaluate variables.

Code: Select all

<form method="post" action="HomePIG.php">
<select name='inv'>
<?php
foreach ($inv as $key => $shw)
{
echo "<option value=\"{$inv[$key]}\">{$inv[$key]}</option>";
}
?>
Also, $inv[$key] is the exacty same as $shw in your example above.

Posted: Thu Dec 14, 2006 7:16 pm
by Mythic Fr0st
Thanks alot... it worked