473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Apr 9 '08 #1
3 1594
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.
js*******@attglobal.net
==================

Apr 9 '08 #2
On Apr 9, 4:35 pm, Jerry Stuckle <jstuck...@attglobal.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='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
Apr 9 '08 #3
On Apr 9, 4:37 pm, jerrygarciuh <jerrygarc...@gmail.comwrote:
On Apr 9, 4:35 pm, Jerry Stuckle <jstuck...@attglobal.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='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!
Apr 9 '08 #4

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

Similar topics

17
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of...
5
by: Shawn Modersohn | last post by:
For the script: <script language="JavaScript"> function pullPage(){ var arrayLength=document.teamSelectionF.teamSelectionS.length; var pageNav = new Array(arrayLength); var...
0
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
11
by: solandre | last post by:
hi there, i have to find matches in two sorted arrays. there are two good possibilities as a temporary solution, performing different, depending on the inner structure of the data. but i try to...
1
by: defcon8 | last post by:
Hello. I have been trying to use PIL's .putdata() method and I have been having some problems. The error I get is: Traceback (most recent call last): File "realautomata.py", line 72, in ?...
3
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work (...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
1
by: CaseySimplified | last post by:
I am writing a LinkedList class from scratch without using the already defined LinkedList class. The only thing that doesn't seem to be working is adding removing and getting the last link in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.