Click here to get back home

Problems with matching on last element in array

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Problems with matching on last element in array jerrygarciuh 04-09-2008
Get Chitika Premium
Posted by jerrygarciuh on April 9, 2008, 3:22 pm
Please log in for more thread options
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

Posted by Jerry Stuckle on April 9, 2008, 5:35 pm
Please log in for more thread options
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='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
>

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@attglobal.net
==================


Posted by jerrygarciuh on April 9, 2008, 5:37 pm
Please log in for more thread options
> 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='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
>
> 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...@attglobal.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='Construction'checked onClick='setBoxes99()'> Construction<br />
<input type='checkbox' name='answer_2_99' id='answer_2_99'
value='Healthcare'checked onClick='setBoxes99()'> Healthcare<br />
<input type='checkbox' name='answer_3_99' id='answer_3_99'
value='Cultural Economy'checked onClick='setBoxes99()'> Cultural
Economy<br />

<input type='checkbox' name='answer_4_99' id='answer_4_99'
value='Manufacturing'checked onClick='setBoxes99()'> Manufacturing<br /
>
<input type='checkbox' name='answer_5_99' id='answer_5_99'
value='Transportation'checked onClick='setBoxes99()'>
Transportation<br />
<input type='checkbox' name='answer_6_99' id='answer_6_99' value='Oil
and Gas' onClick='setBoxes99()'> Oil and Gas

Thanks for your help!

jg

Posted by jerrygarciuh on April 9, 2008, 5:41 pm
Please log in for more thread options
>
>
>
> > 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='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
>
> > 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...@attglobal.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='Construction'checked onClick='setBoxes99()'> Construction<br />
> <input type='checkbox' name='answer_2_99' id='answer_2_99'
> value='Healthcare'checked onClick='setBoxes99()'> Healthcare<br />
> <input type='checkbox' name='answer_3_99' id='answer_3_99'
> value='Cultural Economy'checked onClick='setBoxes99()'> Cultural
> Economy<br />
>
> <input type='checkbox' name='answer_4_99' id='answer_4_99'
> value='Manufacturing'checked onClick='setBoxes99()'> Manufacturing<br /
>
> <input type='checkbox' name='answer_5_99' id='answer_5_99'
> value='Transportation'checked onClick='setBoxes99()'>
> Transportation<br />
> <input type='checkbox' name='answer_6_99' id='answer_6_99' value='Oil
> and Gas' onClick='setBoxes99()'> 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!

Similar ThreadsPosted
Matching more than one value in a file array January 3, 2005, 8:37 am
Pattern Matching When $haystack _may_ be an array June 8, 2007, 1:33 pm
How to get array element? November 4, 2006, 3:08 pm
Does an array contain an element? December 4, 2006, 10:31 am
getting first element of an array March 15, 2008, 12:59 pm
First element is 'Array'? July 13, 2008, 5:15 am
I thought this would be easy: Matching array elements inside of a while loop October 8, 2008, 3:57 pm
How to add element in php array dynamically? August 1, 2004, 10:36 pm
missing array element May 29, 2005, 12:19 pm
Missing element in array?! May 20, 2007, 6:38 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap