473,474 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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>
Jul 17 '05 #1
2 2900
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>

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

Uzytkownik "Mountain Man" <ei*************@yahoo.com> napisal w wiadomosci
news:78**************************@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>

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: Steve Barnett | last post by:
I have a collection which I need to navigate through in several different ways. Is there any way to structure my class such that I can have several different iterators? Ideally, I'd like to do...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
4
by: Jack E Leonard | last post by:
I'm looping through the keys and values of a form submission using foreach($_POST as $key => $value) echo "$key = $value<br>"; No problems there. All works as expected. But seveal of the...
5
by: ano | last post by:
I received this error when I used "ref" in foreach statement. Dictionary<string, object> oInterfaces; foreach (clsInterface vinterface in oInterfaces.Values) { func1( ref vinterface ); } ...
2
by: The.Relinator | last post by:
Hello, I am unable to get the following piece of code to work as desired. The fields valuable seems to contain all values however the vaules variable only contains the first assignied with $values...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
6
by: John A Grandy | last post by:
Is it possible to write a foreach so that it simultaneously iterates through two collections ( or two properties of the same collection ) ? Following is *only meant as an example* : -- iterate...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.