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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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> |
|
|||
|
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> > > |
|
|||
|
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> |