I have a very large orderform, where customers can order different food.
Here is an example of my html (not completely sure if this is the correct way):
<legend>Dinner</legend>
<label for="beef">Beef</label>
<input type="text" id="beef" name="order[]dinner[][beef]" placeholder="How many beef plates do you want?">
<label for="chicken">Chicken</label>
<input type="text" id="chicken" name="order[]dinner[][chicken]" placeholder="How many chicken plates do you want?">
<legend>Desserts</legend>
<label for="cake">Cake</label>
<input type="text" id="cake" name="order[]desserts[][cake]" placeholder="How many cake slices do you want?">
<label for="pudding">Pudding</label>
<input type="text" id="pudding" name="order[]pudding[][cake]" placeholder="How many puddings do you want?">
How could I make a forloop to collect all the orders (where the value is over 0)
I have tried this code:
$order_array = $_POST['order'];
foreach ($order_array AS $key => $value){
$order .= "<p><strong>$key:</strong> $value</p>\n";
}
But the output is this:
0: 30
1: 2
I would like it to be:
Beef: 30
Chicken: 1
Also, if possible could display the “category” like:
Dinner:
Beef: 30
Chicken: 1
Dessert:
Cake: 29
Pudding: 2
Edit:
Here is my post array:
array (
'order' =>
'Dinner' =>
array (
'beef' => '',
'chicken' => '40',
),
'Dessert' =>
array (
'Cake' => '',
'Pudding' => '',
),
),
First, change your inputs so that their names are in the following format:
<input name="order[category][food]" />Then, once the data has been posted you can do something like the following to get the desired output:
Which would output something like the following:-
Dinner
Beef: 12
Chicken: 2
Dessert
Cake: 4
Pudding: 1
Edit: If you need to prevent displaying categories (
$type) from showing if there are no$itemsin them, you can do the following: