foreach is returning value of first key for all keys

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mountain Man

    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_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>
  • Timo Schäfer

    #2
    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_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.

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

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

    cheers,
    moe.



    Mountain Man wrote:
    [color=blue]
    >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.
    >
    >$characteristi cs = 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_ click (posPoints, $posPoints, $characteristic s,
    >$charClick);
    >
    >?>
    >
    ></form>
    >
    >[/color]

    Comment

    • Chung Leong

      #3
      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" <eightypoundpac k@yahoo.com> napisal w wiadomosci
      news:78bbd4ee.0 402251323.6d8ca 5cb@posting.goo gle.com...[color=blue]
      > 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>[/color]


      Comment

      Working...