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='setBoxes
$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='setBoxes
$this->id()'> $a<br />\n";
}

Any thoughts?

TIA!


jg
.



Relevant Pages

  • Re: Problems with matching on last element in array
    ... As part of an app I am validating and repopulating user input from ... $responses is posted checkbox input, ... single box in every single group of boxes EXCEPT for the last one. ...
    (comp.lang.php)
  • Re: Auto Logon for XP
    ... I looked at this procedure and found the Check Boxes were already clear ... no Pop Up Box appeared to allow me to enter my Account Name. ... See the other responses you received for further suggestions to solve your ... into a Google search box and get ten highly relevant references on the very ...
    (microsoft.public.windowsxp.general)
  • Re: Still surplus and now more!
    ... have lost my previous posts and responses. ... Still surplus and needing to go before weekend is the herbert air chuck ... I also have found another air chuck slightly smaller then the Herbert one ... boxes around for long enough. ...
    (uk.rec.models.engineering)
  • Re: Electronic Forms Questionnaires
    ... I've created an electronic form with text and check ... people as a test to get their responses. ... then attach it to an e-mail back to me. ... couldn't type anything in the text boxes). ...
    (microsoft.public.word.docmanagement)
  • Re: Frontpage 2000: Form confirmation
    ... Scrolling text boxes automatically appear at the end. ... >questions using the text scroll boxes and checkboxes. ... >the scroll text box responses were together as well. ... >my Form Confirmation match the actual form on ...
    (microsoft.public.frontpage.addins)