FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » option values with php foreach not working
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
option values with php foreach not working [message #171112] Sat, 25 December 2010 02:58 Go to next message
richard is currently offline  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 Go to previous messageGo to next message
richard is currently offline  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 #171115 is a reply to message #171112] Sat, 25 December 2010 03:29 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
Never mind all of the above.
Finally fixed the problem. Just a matter of where you put stuff.

<body>


<?php

$arr = array("one", "two", "three");
foreach ($arr as $key => $value)
{ echo "Key: $key; Value: $value<br />\n"; }

$myArray=array(1,2,3,4,5);

?>

<select name="menu1" size="10">
<option value="101">adam</option>

<?php

foreach ($myArray as $myItem)
{ echo "<option value='$myItem'>$myItem</option>\n"; }
?>
</select>
</body>
Re: option values with php foreach not working [message #171116 is a reply to message #171112] Sat, 25 December 2010 03:48 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
http://1littleworld.net/sample1.php

Looks fine in firefox, but in IE8 there are embedded line breaks.


I used Liberty Basic to create the array information then simply copied and
pasted.

The list of names shown are a partial list of songs that were on the
airwaves in 1960.
Re: option values with php foreach not working [message #171138 is a reply to message #171112] Mon, 27 December 2010 12:25 Go to previous message
tony is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Slowly coming together
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 16:43:38 GMT 2024

Total time taken to generate the page: 0.04897 seconds