Problems with matching on last element in array

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

    Problems with matching on last element in array

    Hi folks,

    As part of an app I am validating and repopulating user input from
    check boxes. $responses is posted checkbox input, $answers is the
    data used to create and recreate the checkboxes.

    I am having the darned time figuring out why this works for every
    single box in every single group of boxes EXCEPT for the last one.
    Given that it does this persistently across sets of data I know I have
    an error in my logic buy I can't find it.

    I have dumped the data as I iterate over it and it is always exactly
    what I expect.

    Here is print_r() for each array:

    // Here is $answers
    Array
    (
    [0] =Construction
    [1] =Healthcare
    [2] =Cultural Economy
    [3] =Manufacturing
    [4] =Transportation
    [5] =Oil and Gas

    )

    // Here is $responses
    Array
    (
    [0] =Construction
    [1] =Healthcare
    [2] =Cultural Economy
    [3] =Manufacturing
    [4] =Transportation
    [5] =Oil and Gas
    )

    // I have both methods I have used here:

    // METHOD 1
    foreach ($answers as $a) {
    $iter++;
    if ($responses) {
    $ckd = (in_array($a, $responses)) ? 'checked' : '';
    }
    echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
    id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
    $this->id()'$a<br />\n";
    }


    // so when in_array didn't work I also tried this with the same
    results
    // METHOD 2
    foreach ($answers as $a) {
    $iter++;
    if ($responses) {
    $ckd = '';
    foreach ($responses as $r) {
    if ($r == $a) {
    $ckd = 'checked';
    break;
    }
    }
    }echo "$a is a; $ckd is ckd<br>";
    echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
    id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
    $this->id()'$a<br />\n";
    }

    Any thoughts?

    TIA!


    jg
  • Jerry Stuckle

    #2
    Re: Problems with matching on last element in array

    jerrygarciuh wrote:
    Hi folks,
    >
    As part of an app I am validating and repopulating user input from
    check boxes. $responses is posted checkbox input, $answers is the
    data used to create and recreate the checkboxes.
    >
    I am having the darned time figuring out why this works for every
    single box in every single group of boxes EXCEPT for the last one.
    Given that it does this persistently across sets of data I know I have
    an error in my logic buy I can't find it.
    >
    I have dumped the data as I iterate over it and it is always exactly
    what I expect.
    >
    Here is print_r() for each array:
    >
    // Here is $answers
    Array
    (
    [0] =Construction
    [1] =Healthcare
    [2] =Cultural Economy
    [3] =Manufacturing
    [4] =Transportation
    [5] =Oil and Gas
    >
    )
    >
    // Here is $responses
    Array
    (
    [0] =Construction
    [1] =Healthcare
    [2] =Cultural Economy
    [3] =Manufacturing
    [4] =Transportation
    [5] =Oil and Gas
    )
    >
    // I have both methods I have used here:
    >
    // METHOD 1
    foreach ($answers as $a) {
    $iter++;
    if ($responses) {
    $ckd = (in_array($a, $responses)) ? 'checked' : '';
    }
    echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
    id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
    $this->id()'$a<br />\n";
    }
    >
    >
    // so when in_array didn't work I also tried this with the same
    results
    // METHOD 2
    foreach ($answers as $a) {
    $iter++;
    if ($responses) {
    $ckd = '';
    foreach ($responses as $r) {
    if ($r == $a) {
    $ckd = 'checked';
    break;
    }
    }
    }echo "$a is a; $ckd is ckd<br>";
    echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
    id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
    $this->id()'$a<br />\n";
    }
    >
    Any thoughts?
    >
    TIA!
    >
    >
    jg
    >
    What's the initial value of $iter? And what is the result of running
    the code - i.e. the generated source?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • jerrygarciuh

      #3
      Re: Problems with matching on last element in array

      On Apr 9, 4:35 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
      jerrygarciuh wrote:
      Hi folks,
      >
      As part of an app I am validating and repopulating user input from
      check boxes. $responses is posted checkbox input, $answers is the
      data used to create and recreate the checkboxes.
      >
      I am having the darned time figuring out why this works for every
      single box in every single group of boxes EXCEPT for the last one.
      Given that it does this persistently across sets of data I know I have
      an error in my logic buy I can't find it.
      >
      I have dumped the data as I iterate over it and it is always exactly
      what I expect.
      >
      Here is print_r() for each array:
      >
      // Here is $answers
      Array
      (
      [0] =Construction
      [1] =Healthcare
      [2] =Cultural Economy
      [3] =Manufacturing
      [4] =Transportation
      [5] =Oil and Gas
      >
      )
      >
      // Here is $responses
      Array
      (
      [0] =Construction
      [1] =Healthcare
      [2] =Cultural Economy
      [3] =Manufacturing
      [4] =Transportation
      [5] =Oil and Gas
      )
      >
      // I have both methods I have used here:
      >
      // METHOD 1
      foreach ($answers as $a) {
      $iter++;
      if ($responses) {
      $ckd = (in_array($a, $responses)) ? 'checked' : '';
      }
      echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
      id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
      $this->id()'$a<br />\n";
      }
      >
      // so when in_array didn't work I also tried this with the same
      results
      // METHOD 2
      foreach ($answers as $a) {
      $iter++;
      if ($responses) {
      $ckd = '';
      foreach ($responses as $r) {
      if ($r == $a) {
      $ckd = 'checked';
      break;
      }
      }
      }echo "$a is a; $ckd is ckd<br>";
      echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
      id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
      $this->id()'$a<br />\n";
      }
      >
      Any thoughts?
      >
      TIA!
      >
      jg
      >
      What's the initial value of $iter? And what is the result of running
      the code - i.e. the generated source?
      >
      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstuck...@attgl obal.net
      =============== ===
      Hi Jerry,

      Thanks for the reply!

      $iter = 0 initially.

      Here is an example: http://www.novacvideo.org/registration/form.php?id=2

      Checkbox output code looks like this:

      <input type='checkbox' name='answer_1_ 99' id='answer_1_99 '
      value='Construc tion'checked onClick='setBox es99()'Construc tion<br />
      <input type='checkbox' name='answer_2_ 99' id='answer_2_99 '
      value='Healthca re'checked onClick='setBox es99()'Healthca re<br />
      <input type='checkbox' name='answer_3_ 99' id='answer_3_99 '
      value='Cultural Economy'checked onClick='setBox es99()'Cultural
      Economy<br />

      <input type='checkbox' name='answer_4_ 99' id='answer_4_99 '
      value='Manufact uring'checked onClick='setBox es99()'Manufact uring<br /
      >
      <input type='checkbox' name='answer_5_ 99' id='answer_5_99 '
      value='Transpor tation'checked onClick='setBox es99()'>
      Transportation< br />
      <input type='checkbox' name='answer_6_ 99' id='answer_6_99 ' value='Oil
      and Gas' onClick='setBox es99()'Oil and Gas

      Thanks for your help!

      jg

      Comment

      • jerrygarciuh

        #4
        Re: Problems with matching on last element in array

        On Apr 9, 4:37 pm, jerrygarciuh <jerrygarc...@g mail.comwrote:
        On Apr 9, 4:35 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        >
        >
        >
        jerrygarciuh wrote:
        Hi folks,
        >
        As part of an app I am validating and repopulating user input from
        check boxes. $responses is posted checkbox input, $answers is the
        data used to create and recreate the checkboxes.
        >
        I am having the darned time figuring out why this works for every
        single box in every single group of boxes EXCEPT for the last one.
        Given that it does this persistently across sets of data I know I have
        an error in my logic buy I can't find it.
        >
        I have dumped the data as I iterate over it and it is always exactly
        what I expect.
        >
        Here is print_r() for each array:
        >
        // Here is $answers
        Array
        (
        [0] =Construction
        [1] =Healthcare
        [2] =Cultural Economy
        [3] =Manufacturing
        [4] =Transportation
        [5] =Oil and Gas
        >
        )
        >
        // Here is $responses
        Array
        (
        [0] =Construction
        [1] =Healthcare
        [2] =Cultural Economy
        [3] =Manufacturing
        [4] =Transportation
        [5] =Oil and Gas
        )
        >
        // I have both methods I have used here:
        >
        // METHOD 1
        foreach ($answers as $a) {
        $iter++;
        if ($responses) {
        $ckd = (in_array($a, $responses)) ? 'checked' : '';
        }
        echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
        id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
        $this->id()'$a<br />\n";
        }
        >
        // so when in_array didn't work I also tried this with the same
        results
        // METHOD 2
        foreach ($answers as $a) {
        $iter++;
        if ($responses) {
        $ckd = '';
        foreach ($responses as $r) {
        if ($r == $a) {
        $ckd = 'checked';
        break;
        }
        }
        }echo "$a is a; $ckd is ckd<br>";
        echo " <input type='checkbox' name='answer_" . $iter . "_$this->id'
        id='answer_" . $iter . "_$this->id' value='$a' $ckd onClick='setBox es
        $this->id()'$a<br />\n";
        }
        >
        Any thoughts?
        >
        TIA!
        >
        jg
        >
        What's the initial value of $iter? And what is the result of running
        the code - i.e. the generated source?
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===
        >
        Hi Jerry,
        >
        Thanks for the reply!
        >
        $iter = 0 initially.
        >
        Here is an example: http://www.novacvideo.org/registration/form.php?id=2
        >
        Checkbox output code looks like this:
        >
        <input type='checkbox' name='answer_1_ 99' id='answer_1_99 '
        value='Construc tion'checked onClick='setBox es99()'Construc tion<br />
        <input type='checkbox' name='answer_2_ 99' id='answer_2_99 '
        value='Healthca re'checked onClick='setBox es99()'Healthca re<br />
        <input type='checkbox' name='answer_3_ 99' id='answer_3_99 '
        value='Cultural Economy'checked onClick='setBox es99()'Cultural
        Economy<br />
        >
        <input type='checkbox' name='answer_4_ 99' id='answer_4_99 '
        value='Manufact uring'checked onClick='setBox es99()'Manufact uring<br /
        >
        <input type='checkbox' name='answer_5_ 99' id='answer_5_99 '
        value='Transpor tation'checked onClick='setBox es99()'>
        Transportation< br />
        <input type='checkbox' name='answer_6_ 99' id='answer_6_99 ' value='Oil
        and Gas' onClick='setBox es99()'Oil and Gas
        >
        Thanks for your help!
        >
        jg

        Arg! I found the error! There was a newline character in the
        database! It was throwing everything in both test datasets!

        Thanks Jerry, I had to look at the output to see it!

        Comment

        Working...