469,579 Members | 1,899 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

array Issue - Correspoing checkbox and textfield values to behave

Hello All,

I'm trying to figure out this array problem I have. I don't know if
I'm doing this correctly. Anyone want to take a look.

---------------------
Current Situation.
---------------------
I have a form with checkboxes and corresponding textfields for each
checkbox. Here is the HTML

<input type="checkbox" name="shows[]" value="Morning">Morning</
option># of People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Lunch">Lunch</option># of
People<input type="text" name="people[]" value="" />
<input type="checkbox" name="shows[]" value="Evening">Evening</
option># of People<input type="text" name="people[]" value="" />

---------------------
Current Problem
---------------------
If I was to fill out the form and check off the "Evening" checkbox
with 15 people in the textbox and looked at the arrays I would have
the following:

shows Array {
[0] =Evening
}

people Array {
[0] =>
[1] =>
[2] =15
}

I guess I was expecting this

shows Array {
[0] =Evening
}

people Array {
[0] =15
}
---------------------
Attempted Solution
---------------------

// Merge Shows Options
function show_Pairing($s, $n) {
return("$s <brest. $n People,<br><br>");
}

function map_Pairs($s, $n) {
return(array($s =$n));
}

$merged_list = array_map("show_Pairing", $_POST["shows"],
$_POST["people"]);

foreach ($merged_list as $key =$value) {
$shows_list .= ucfirst ($key) ." : ". $value . "<br>";
}
---------------------
Result
---------------------

0. Evening est. people
1. est. people
2. est. 15 people
So what's happening is the checkboxes seem to have their own key
values and the text boxes have their own key values but when I pair
them they don't seem to want to match.
In this example "Evening" should have a key of shows[2] because that's
how it appeared in the list HTML form but that's not happening.
Is there a PHP solution that can deal with this? Do I need to
reconsider how I laid out my HTML?
Looking for a PHP 4.4 solution considering ISP doesn't have PHP 5.

Jul 18 '07 #1
2 2761
| <input type="checkbox" name="shows[]" value="Morning">Morning</
| option># of People<input type="text" name="people[]" value="" />
| <input type="checkbox" name="shows[]" value="Lunch">Lunch</option># of
| People<input type="text" name="people[]" value="" />
| <input type="checkbox" name="shows[]" value="Evening">Evening</
| option># of People<input type="text" name="people[]" value="" />
|
| ---------------------
| Current Problem
| ---------------------
| If I was to fill out the form and check off the "Evening" checkbox
| with 15 people in the textbox and looked at the arrays I would have
| the following:
|
| shows Array {
| [0] =Evening
| }
|
| people Array {
| [0] =>
| [1] =>
| [2] =15
| }

seems about right. ALL 'text' type inputs will submit. only SELECTED
'checkbox'es submit. just change the html...

<input name='showTime[morning]' type='checkbox' value='morning'>
# of ppl <input name='people[morning]' type='text' value='0'>
<input name='showTime[lunch]' type='checkbox' value='lunch'>
# of ppl <input name='people[lunch]' type='text' value='0'>
<input name='showTime[evening]' type='checkbox' value='evening'>
# of ppl <input name='people[evening]' type='text' value='0'>

now try this:

foreach ($showTime as $timeOfDay =$value)
{
$attendees = $people[$timeOfDay]
if (!$attendees){ continue; }
echo '<pre>' .
($timeOfDay == 'lunch' ? 'At ' : 'In the ') .
$timeOfDay . ', there will be ' . $attendees .
' attendee' . ($attendees == 1 ? '' : 's') .
' coming to see the show.' .
'</pre>';
}

that's pretty straight-forward.
Jul 19 '07 #2
| foreach ($showTime as $timeOfDay =$value)
| {
| $attendees = $people[$timeOfDay]

and don't forget that semi-colon. :)
Jul 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by damjanu | last post: by
6 posts views Thread by Chuck Anderson | last post: by
1 post views Thread by assgar | last post: by
pradeepjain
1 post views Thread by pradeepjain | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.