从数组计算总价

时间:2014-12-09 17:44:06

标签: php arrays sum

我目前在一个外卖餐厅的网站上工作,我有一个菜单页面,客户将他们选择的食物添加到购物车,当他们点击“添加到购物车”时,他们被发送到显示他们添加的项目。

目前,会显示商品名称,价格和数量,但我希望在购物车底部添加总费用金额。

以下是我的菜单页面上的相关代码:

<h2> Just a bite </h2>
 <div id="table1">
     <table border="0" cellspacing="0" cellpadding="2" width="500">
         <tr>
           <td colspan="7" class="pageName">Please select from our starters below</td>
           <br>
         </tr>
         <tr>
           <td width="22%" height="110"><img src="shrimp.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
           <td width="22%" height="110"><img src="potatoskins.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
           <td width="22%" height="110"><img src="salad.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
           <td>&nbsp;</td>
      <      td width="22%" height="110"><img src="bread.jpg" alt="small product photo" width="110" height="110" border="0" /></td>
        </tr>
         <tr>
           <td class="detailText" valign="top" nowrap="nowrap"> Sticky BBQ Prawns<br><br>
           £5.00 <br><br> <a href="order.php?add=Sticky BBQ Prawns&price=5.00&qty=1"><button>Add to Cart</button></a><br><br><img src="1chilli.png" width="20" height="20"></td>
           <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Potato Skins<br><br>
             £4.00 <br><br> <a href="order.php?add=Potato Skins&price=4.00&qty=1"><button>Add to Cart</button></a></td>
          <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Caesar Salad<br><br>
            £3.00 <br><br> <a href="order.php?add=Caesar Salad&price=3.00&qty=1"><button>Add to Cart</button></a><br><br><img src="vege.png" width="20" height="20"></td>
          <td>&nbsp;</td>
            <td class="detailText" valign="top" nowrap="nowrap"> Fresh Bread Selection<br><br>
            £1.00 <br><br> <a href="order.php?add=Fresh Bread Selection&price=1.00&qty=1"><button>Add to Cart</button></a><br><br><img src="vege.png" width="20" height="20"></td>
        </tr>
         <tr>
             <td colspan="7">&nbsp;</td>
         </tr>
         </table>
 </div>

以下是我购物车页面上的相关代码,包括我希望显示总价格的区域:

<?php
session_start();

if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }


if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
    $ITEM = array(
        'name' => $_GET['add'], 
        'price' => $_GET['price'], 
        'qty' => $_GET['qty']       
        );

    $_SESSION['SHOPPING_CART'][] =  $ITEM;
    header('Location: ' . $_SERVER['PHP_SELF']);
}
else if (isset($_GET['remove'])){
    unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
    header('Location: ' . $_SERVER['PHP_SELF']);

}
else if (isset($_GET['empty'])){
    session_destroy();
    header('Location: ' . $_SERVER['PHP_SELF']);

}
else if (isset($_POST['update'])) {
    foreach ($_POST['items_qty'] as $itemID => $qty) {
        if ($qty == 0) {
            unset($_SESSION['SHOPPING_CART'][$itemID]);
        }
        else if($qty >= 1) {
            $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
        }
    }
    header('Location: ' . $_SERVER['PHP_SELF']);
}
?>

...

<div id="shoppingCartDisplay">
<form action="" method="post" name="shoppingcart">
    <table width="680" border="2">
      <tr>
        <th scope="col">Remove Item/s</th>
        <th scope="col">Item Name</th>
        <th scope="col">Item Price</th>
        <th scope="col">Qty</th>
        <th scope="col">Cost</th>
      </tr>

        <?php 
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td><a href="?remove=<?php echo $itemNumber; ?>"><img src="x.png"></a></td>
            <td><?php echo $item['name']; ?></td>
            <td>£<?php echo $item['price']; ?></td>
            <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="3" maxlength="3" /></td>
            <td>£<?php echo $item['qty'] * $item['price']; ?></td>
        </tr>
        <?php
        }
        ?>
    </table>
    Total price: <?php echo $itemNumber['price'] + $item['price']; ?>
    <?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
    <p>
      <label>
      <input type="submit" name="update" id="update" value="Update Cart" />
      </label>
    </p>
</form>

正如你所看到的,我试图尝试这项工作,但我真的不确定:(如果有人有任何建议我会很感激帮助!谢谢:)

1 个答案:

答案 0 :(得分:2)

$ itemNumber和$ item只能在foreach中访问。

    <?php $totalPrice = 0; ?>
    <?php 
    foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
    ?>
    <tr id="item<?php echo $itemNumber; ?>">    
        <td><a href="?remove=<?php echo $itemNumber; ?>"><img src="x.png"></a></td>
        <td><?php echo $item['name']; ?></td>
        <td>£<?php echo $item['price']; ?></td>
        <td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="3" maxlength="3" /></td>
        <td>£<?php echo $item['qty'] * $item['price']; ?></td>
        <?php $totalPrice += (($item['qty']>=0?$item['qty']:0) * $item['price']); ?>
    </tr>
    <?php
    }
    ?>
</table>
Total price: <?php echo $totalPrice; ?>

我完全赞同杰森。您应该从get参数中删除价格。 它们很容易被用来免费订购。 如果量化不是负数,也要检查。