?
Solved

Multi Dimensional Array in Form from check boxes

Posted on 2005-03-02
1
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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