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_c lick ($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($valu e, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}
# End of user defined function.
$characteristic s = 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('Are n\'t they the greatest?');",
'minds' => "alert('They\'r e smarter than most people their age.');",
);
make_checkbox_c lick (posPoints, $posPoints, $characteristic s,
$charClick);
?>
</form>
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_c lick ($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($valu e, $query)) { echo "checked "; }
echo "/> $label</label><br />\n";
}
}
# End of user defined function.
$characteristic s = 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('Are n\'t they the greatest?');",
'minds' => "alert('They\'r e smarter than most people their age.');",
);
make_checkbox_c lick (posPoints, $posPoints, $characteristic s,
$charClick);
?>
</form>
Comment