foreach is returning value of first key for all keys

This is a discussion on foreach is returning value of first key for all keys within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2004
Mountain Man
 
Posts: n/a
Default foreach is returning value of first key for all keys

Hi,

I'm having trouble with the foreach function. I'm using it twice
inside a user defined function with two different arrays, and in the
second instance it's returning the value of the first key for all the
keys. My code is shown below and the problem areas are marked with
comments. In case you're wondering, this script is for generating
sticky checkboxes that include an event handler. Many thanks to anyone
who can help.

Mountain Man

<form>

<?php

if (! is_array($like)) { $like = array(); }

function make_checkbox_click ($name, $query, $options, $onClick) {
foreach ($options as $value => $label) {
printf('<label><input type="checkbox" name="%s[]" value="%s" ',
$name, $value);

# This is the instance of foreach that's not working correctly.
# It's returning the value of the first key for all the keys.

foreach ($onClick as $key => $event) {
printf ('onClick="%s" ', $event);
}

if (in_array($value, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}

# End of user defined function.

$characteristics = array(
'personality' => ' I love their personalities.',
'minds' => ' I admire their minds.',
);

# This is the array that foreach isn't working correctly with.

$charClick = array (
'personality' => "alert('Aren\'t they the greatest?');",
'minds' => "alert('They\'re smarter than most people their age.');",
);

make_checkbox_click (posPoints, $posPoints, $characteristics,
$charClick);

?>

</form>
Reply With Quote
  #2 (permalink)  
Old 02-25-2004
Timo Schäfer
 
Posts: n/a
Default Re: foreach is returning value of first key for all keys

hi moutain,

please take a look at the source code of your results - 'onClick' has
been generated twice for each option.
i think you intended something like that:

function make_checkbox_click ($name, $query, $options, $onClick) {
foreach ($options as $value => $label) {
printf('<label><input type="checkbox" name="%s[]" value="%s"
',$name, $value);

# This is the instance of foreach that's not working correctly.
# It's returning the value of the first key for all the keys.

printf ('onClick="%s" ', $onClick[$value]);

if (in_array($value, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}

cheers,
moe.



Mountain Man wrote:

>Hi,
>
>I'm having trouble with the foreach function. I'm using it twice
>inside a user defined function with two different arrays, and in the
>second instance it's returning the value of the first key for all the
>keys. My code is shown below and the problem areas are marked with
>comments. In case you're wondering, this script is for generating
>sticky checkboxes that include an event handler. Many thanks to anyone
>who can help.
>
>Mountain Man
>
><form>
>
><?php
>
>if (! is_array($like)) { $like = array(); }
>
>function make_checkbox_click ($name, $query, $options, $onClick) {
>foreach ($options as $value => $label) {
> printf('<label><input type="checkbox" name="%s[]" value="%s" ',
> $name, $value);
>
># This is the instance of foreach that's not working correctly.
># It's returning the value of the first key for all the keys.
>
>foreach ($onClick as $key => $event) {
> printf ('onClick="%s" ', $event);
> }
>
> if (in_array($value, $query)) { echo "checked "; }
> echo "/> $label</label><br />\n";
>}
>}
>
># End of user defined function.
>
>$characteristics = array(
> 'personality' => ' I love their personalities.',
> 'minds' => ' I admire their minds.',
> );
>
># This is the array that foreach isn't working correctly with.
>
>$charClick = array (
> 'personality' => "alert('Aren\'t they the greatest?');",
> 'minds' => "alert('They\'re smarter than most people their age.');",
> );
>
>make_checkbox_click (posPoints, $posPoints, $characteristics,
>$charClick);
>
>?>
>
></form>
>
>

Reply With Quote
  #3 (permalink)  
Old 02-26-2004
Chung Leong
 
Posts: n/a
Default Re: foreach is returning value of first key for all keys

You don't need the second foreach(). $onClick[$value] would yield the
onclick handler for that checkbox.

Uzytkownik "Mountain Man" <eightypoundpack@yahoo.com> napisal w wiadomosci
news:78bbd4ee.0402251323.6d8ca5cb@posting.google.c om...
> Hi,
>
> I'm having trouble with the foreach function. I'm using it twice
> inside a user defined function with two different arrays, and in the
> second instance it's returning the value of the first key for all the
> keys. My code is shown below and the problem areas are marked with
> comments. In case you're wondering, this script is for generating
> sticky checkboxes that include an event handler. Many thanks to anyone
> who can help.
>
> Mountain Man
>
> <form>
>
> <?php
>
> if (! is_array($like)) { $like = array(); }
>
> function make_checkbox_click ($name, $query, $options, $onClick) {
> foreach ($options as $value => $label) {
> printf('<label><input type="checkbox" name="%s[]" value="%s" ',
> $name, $value);
>
> # This is the instance of foreach that's not working correctly.
> # It's returning the value of the first key for all the keys.
>
> foreach ($onClick as $key => $event) {
> printf ('onClick="%s" ', $event);
> }
>
> if (in_array($value, $query)) { echo "checked "; }
> echo "/> $label</label><br />\n";
> }
> }
>
> # End of user defined function.
>
> $characteristics = array(
> 'personality' => ' I love their personalities.',
> 'minds' => ' I admire their minds.',
> );
>
> # This is the array that foreach isn't working correctly with.
>
> $charClick = array (
> 'personality' => "alert('Aren\'t they the greatest?');",
> 'minds' => "alert('They\'re smarter than most people their age.');",
> );
>
> make_checkbox_click (posPoints, $posPoints, $characteristics,
> $charClick);
>
> ?>
>
> </form>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0