我的菜单中有三个项目。我想计算不同数量的每个项目的总数。但我的foreach循环是用所有3个数量而不是一个具有给定数量的项目计算每个项目。请解释我在哪里做错了。这是我的代码。
<?php
if(isset($_POST['bill'])){
//foreach ($_POST['qty'] as $item_quantity) {
//$item_qty = $item_quantity;
//}
?>
<div class="row calc">
<div class="col-md-offset-4 col-md-4 col-md-offset-4">
<center><h4>Geektroup Hotels</h4>
<table class="table table-bordered">
<thead>
<th>Sr #</th>
<th>Item Name</th>
<th>Item Cost</th>
<th>Quantity</th>
<th>Amount</th>
</thead>
<tbody>
<?php
$count = 0;
**foreach ($_POST['item_id'] as $item_id) {
$item_num = $item_id;
foreach ($_POST['qty'] as $item_quantity) {
$item_qty = $item_quantity;**
$bill_query = "SELECT * FROM items WHERE item_id = '$item_num'";
$bill_run = mysqli_query($con,$bill_query);
if(mysqli_num_rows($bill_run)>0){
$bill_row = mysqli_fetch_array($bill_run);
$item = $bill_row['item_name'];
$item_price = $bill_row['item_cost'];
$count = $count+1;
?>
<tr>
<td><?php echo $count;?></td>
<td><p><?php echo $item;?></p></td>
<td><p><?php echo $item_price;?></p></td>
<td><p><?php echo $item_qty;?></p></td>
<td><p><?php echo $amount = $item_price * $item_qty;?></p></td>
</tr>
<?php
}
}
}
}
?>
</tbody>
</table>
</center>
</div>
</div>
</div><!-- /.container -->
答案 0 :(得分:-1)
你的html输出不能在第二个foreach循环中。