option values with php foreach not working [message #171112] |
Sat, 25 December 2010 02:58  |
 |
richard
 Messages: 213 Registered: June 2013
Karma: 0
|
Senior Member |
|
|
When I run this code locally I get, in firefox, a listbox with "adam" as
the only output. Nothing else. The second array doesn't even show.
In IE, both items show but I get a warning saying "invalid argument -
foreach".
The second array shows up after the warning.
What am I missing?
Dear Santa,
Please bring lots of coal! We need the heat!
<select name="menu1" size="10">
<option value="101">adam</option>
<?php
$myArray="(1,2,3,4,5)";
foreach ($myArray as $myItem) {
echo "<option value='$myItem'>$myItem</option>\n"; }
$arr = array("one", "two", "three");
foreach ($arr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
?>
</select>
|
|
|
Re: option values with php foreach not working [message #171114 is a reply to message #171112] |
Sat, 25 December 2010 03:25   |
 |
richard
 Messages: 213 Registered: June 2013
Karma: 0
|
Senior Member |
|
|
On Fri, 24 Dec 2010 19:58:56 -0700, richard wrote:
> When I run this code locally I get, in firefox, a listbox with "adam" as
> the only output. Nothing else. The second array doesn't even show.
>
> In IE, both items show but I get a warning saying "invalid argument -
> foreach".
> The second array shows up after the warning.
>
> What am I missing?
>
> Dear Santa,
>
> Please bring lots of coal! We need the heat!
>
>
>
>
> <select name="menu1" size="10">
> <option value="101">adam</option>
>
> <?php
>
> $myArray="(1,2,3,4,5)";
> foreach ($myArray as $myItem) {
> echo "<option value='$myItem'>$myItem</option>\n"; }
>
> $arr = array("one", "two", "three");
> foreach ($arr as $key => $value) {
> echo "Key: $key; Value: $value<br />\n";
> }
>
>
> ?>
>
>
> </select>
Notice I had the second array where it shouldn't be and moved it out of
that part.
Still I get a warning for an invalid argument foreach.
|
|
|
|
|
Re: option values with php foreach not working [message #171138 is a reply to message #171112] |
Mon, 27 December 2010 12:25  |
tony
Messages: 19 Registered: December 2010
Karma: 0
|
Junior Member |
|
|
In article <1fvzvt4txaqp2(dot)dlg(at)evanplatt(dot)sux>,
richard <member(at)newsguy(dot)com> wrote:
> When I run this code locally I get, in firefox, a listbox with "adam" as
> the only output. Nothing else. The second array doesn't even show.
>
> In IE, both items show but I get a warning saying "invalid argument -
> foreach".
> The second array shows up after the warning.
>
> What am I missing?
This is the problem line:
> $myArray="(1,2,3,4,5)";
That doesn't put an array into $myArray, but just a string.
What you need is this:
$myArray = array(1,2,3,4,5);
Cheers
Tony
--
Tony Mountifield
Work: tony(at)softins(dot)co(dot)uk - http://www.softins.co.uk
Play: tony(at)mountifield(dot)org - http://tony.mountifield.org
|
|
|