Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

option values with php foreach not working

 
   Database Help (Home) -> PHP RSS
Next:  do u like new  
Author Message
richard

External


Since: Aug 22, 2008
Posts: 13



(Msg. 1) Posted: Fri Dec 24, 2010 7:58 pm
Post subject: option values with php foreach not working
Archived from groups: comp>lang>php (more info?)

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>

 >> Stay informed about: option values with php foreach not working 
Back to top
Login to vote
richard

External


Since: Aug 22, 2008
Posts: 13



(Msg. 2) Posted: Fri Dec 24, 2010 8:25 pm
Post subject: Re: option values with php foreach not working [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.

 >> Stay informed about: option values with php foreach not working 
Back to top
Login to vote
richard

External


Since: Aug 22, 2008
Posts: 13



(Msg. 3) Posted: Fri Dec 24, 2010 8:29 pm
Post subject: Re: option values with php foreach not working [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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>
 >> Stay informed about: option values with php foreach not working 
Back to top
Login to vote
richard

External


Since: Aug 22, 2008
Posts: 13



(Msg. 4) Posted: Fri Dec 24, 2010 8:48 pm
Post subject: Re: option values with php foreach not working [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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.
 >> Stay informed about: option values with php foreach not working 
Back to top
Login to vote
Tony Mountifield

External


Since: Dec 27, 2010
Posts: 1



(Msg. 5) Posted: Mon Dec 27, 2010 8:26 am
Post subject: Re: option values with php foreach not working [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article ,
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?

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.RemoveThis@softins.co.uk - http://www.softins.co.uk
Play: tony.RemoveThis@mountifield.org - http://tony.mountifield.org
 >> Stay informed about: option values with php foreach not working 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
can you foreach() two arrays at once ? - Hello, So, I was wondering how to do this: foreach($foo as $k=>$v AND $bar as $k2=>$v2){ echo '<TR><TD>$k</TD><TD>$v</TD><TD>$k2</TD><TD>$v2</TD></TR>; } Thanks,

foreach and str_replace - So I've been trying to get a bit of code to: Read all of the files in a dir called thumbs, but not the . and .., use the filename in a link to get the same filename in an images dir. Now I'm trying to use a foreach and glob as suggested, and get rid of...

Variable Array Name in foreach - Hi, I'm trying to use nested foreach loops to loop through two arrays. Each element in the first array corresponds to another array. The brandcode part of the first array is the same as the name of the corresponding array. Basically, one array is of the....

Foreach displaying data twice - I must be missing something rather obvious. I have the following snippet of code that echo's my result twice when it should be echoing just once (only one element in the array). What am I overlooking? ******************************** $OpenQ =..

Why not use a foreach loop instead of having a function ca.. - I was reading this article yesterday: http://www.talkphp.com/advanced-php-programming/1886-how-would-i-apply-htmlentities-every-array-item.html I am wondering.. okay.. we can use array_walk but doesn't that just loop through the items anyway ?? So why..
   Database Help (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]