Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Multi Dimensional Array in Form from check boxes

Posted on 2005-03-02
1
Medium Priority
?
213 Views
Last Modified: 2012-05-05
Hi,

I have a form setup to use check boxes.
On this form I have an array text input for a persons name
<input name=\"contact_name[]\" type=\"text\">
The I have a list of some checkboxes dynamically created from the db, say five options:
<input type=\"checkbox\" name=\"favourite_food[]\" value=\"$result[food_id]\">$result[food_name]

I want to then print the persons name, and then the food s/he likes, according to what was ticked

At the moment It is printing the persons name, then all foods that were ticked, not just the foods that are relative to each specific person.

Basically, I dont know how I need to index the above arrays so that they foods belong to one person, and how would i do the foreach for this ?
0
Comment
Question by:Delacourt
1 Comment
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 2000 total points
ID: 13439218
Looks like you need to name your fields like this:
contact_name[]
favourite_food[0][]
favourite_food[0][]
favourite_food[0][]
...
contact_name[]
favourite_food[1][]
favourite_food[1][]
favourite_food[1][]

Then you can loop through the results like this:
foreach ($contact_name as $key=>$name) {
  echo $name;
  foreach ($favourite_food[$key] as $food) {
    echo $food;
  }
}
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question