1

我有一个非常大的订单,客户可以在其中订购不同的食物。

这是我的 html 示例(不完全确定这是否正确):

<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?">

我怎么能做一个forloop来收集所有的订单(值超过0)

我试过这段代码:

$order_array = $_POST['order'];

foreach ($order_array AS $key => $value){
    $order .= "<p><strong>$key:</strong> $value</p>\n";
}

但输出是这样的:

0:30
1:2

我希望它是:

牛肉:30
鸡肉:1

此外,如果可能的话,可以显示“类别”,如:

晚餐:
牛肉:30
鸡肉:1

甜点:
蛋糕:29
布丁:2

编辑:

这是我的帖子数组:

大批 (
  '订单' =>
    '晚餐' =>
    大批 (
      '牛肉' => '',
      '鸡' => '40',
    ),
    '甜点' =>
    大批 (
      '蛋糕' => '',
      '布丁' => '',
    ),
  ),
4

3 回答 3

0
<form>
<legend>Dinner</legend>
<label for="beef">Beef</label>
<input type="text" id="beef" name="order[beef]" placeholder="How many beef plates do you want?">

<label for="chicken">Chicken</label>
<input type="text" id="chicken" name="order[chicken]" placeholder="How many chicken plates do you want?">


<legend>Desserts</legend>
<label for="cake">Cake</label>
<input type="text" id="cake" name="order[cake]" placeholder="How many cake slices do you want?">

<label for="pudding">Pudding</label>
<input type="text" id="pudding" name="order[pudding]" placeholder="How many puddings do you want?">
<input type="submit" />
</form>
<?php
 $order_array = $_GET['order'];

print_r($order_array);
foreach ($order_array AS $key => $value){
    echo $key;
    echo $value;
}
于 2013-01-24T11:37:33.267 回答
0

首先,更改您的输入,使其名称采用以下格式:

<input name="order[category][food]" />

然后,一旦发布了数据,您就可以执行以下操作来获得所需的输出:

if (!empty($_POST['order'])) {

  foreach ($_POST['order'] as $type => $items) {
    echo '<strong>' . ucfirst($type) . '</strong><br />';

    foreach ($items as $name => $qty) {
      echo ucfirst($type) . ': ' . $qty . '<br />';
    }
    echo '<br />';
  }
}

这将输出如下内容: -

晚餐
牛肉:12
鸡肉:2

甜点
蛋糕:4
布丁:1


编辑:如果您需要阻止显示类别 ( $type) 如果其中没有则显示$items,您可以执行以下操作:

if (count(array_filter($items))) {
  echo '<strong>' . ucfirst($type) . '</strong><br />';
}
于 2013-01-24T11:50:31.330 回答
-1

下面的代码适用于:

牛肉:30

鸡:1

<form>
<legend>Dinner</legend>
<label for="beef">Beef</label>
<input type="text" id="beef" name="order[beef]" placeholder="How many beef plates do you want?">

<label for="chicken">Chicken</label>
<input type="text" id="chicken" name="order[chicken]" placeholder="How many chicken plates do you want?">

<legend>Desserts</legend>
<label for="cake">Cake</label>
<input type="text" id="cake" name="order[cake]" placeholder="How many cake slices do you want?">

<label for="pudding">Pudding</label>
<input type="text" id="pudding" name="order[pudding]" placeholder="How many puddings do you want?">

<input type="submit" name="submit" value="submit"/>
</form>

PHP代码:

<?php
if(isset($_REQUEST['submit']))
{
    $order_array = $_POST['order'];

    $order = "";
    foreach ($order_array AS $key => $value){
        $order .= "<p><strong>$key:</strong> $value</p>\n";
    }

   echo "<pre>";
   print_r($order);
}
?>
于 2013-01-24T11:46:44.453 回答