-2

Hi all I have a shopping cart which is made up of 2 PHP files. order.php and products.php.

All of the configuration is set in the order file and it doesn't use a MYSQL database as im using sessions.

Im trying to get the grand total of all items that are added to the cart however this is what im struggling with. I can get the total of each item added to the cart but not the total of all items together. Any help with this would be greatly appreciated.

Thankyou

PHP

<?php

//Start the session
session_start();

//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }


//Add an item only if we have the threee required pices of information: name, price, qty
if (isset($_GET['add']) && isset($_GET['price']) && isset($_GET['qty'])){
        //Adding an Item
        //Store it in a Array
        $ITEM = array(
                //Item name            
                'name' => $_GET['add'],
                //Item Price
                'price' => $_GET['price'],
                //Qty wanted of item
                'qty' => $_GET['qty']          
                );

        //Add this item to the shopping cart
        $_SESSION['SHOPPING_CART'][] =  $ITEM;
        //Clear the URL variables
        header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
        //Remove the item from the cart
        unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
        //Re-organize the cart
        //array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
        //Clear the URL variables
        header('Location: ' . $_SERVER['PHP_SELF']);

}
else if (isset($_GET['empty'])){
        //Clear Cart by destroying all the data in the session
        session_destroy();
        //Clear the URL variables
        header('Location: ' . $_SERVER['PHP_SELF']);

}
else if (isset($_POST['update'])) {
        //Updates Qty for all items
        foreach ($_POST['items_qty'] as $itemID => $qty) {
                //If the Qty is "0" remove it from the cart
                if ($qty == 0) {
                        //Remove it from the cart
                        unset($_SESSION['SHOPPING_CART'][$itemID]);
                }
                else if($qty >= 1) {
                        //Update to the new Qty
                        $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
                }
        }
        //Clear the POST variables
        header('Location: ' . $_SERVER['PHP_SELF']);
}
?>

HTML FORM

<form action="" method="post" name="shoppingcart">
        <?php
    ob_start();
    ?>
    <table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <th width="143" height="46" scope="col">&nbsp;</th>
        <th width="177" scope="col">Item Name</th>
        <th width="155" scope="col">Unit Price</th>
        <th width="130" scope="col">Quantity</th>
        <th width="95" scope="col">Total</th>
      </tr>

        <?php
        //Print all the items in the shopping cart
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td height="41"><a href="?remove=<?php echo $itemNumber; ?>">Remove Item</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="2" maxlength="3" /></td>
            <td>£<?php echo $item['qty'] * $item['price']; ?></td>  
            <td>£<?php echo $item['total'] * $item['price']; ?></td>      
        </tr>
        <?php
        }
        ?>
    </table>
        <?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
    <p align="left">
      <label>

      <p><a href="../products.php">Keep Shopping</a> - <a href="?empty">Empty Cart</a><br />
      <br />
<input type="submit" name="update" id="update" value="Update Cart" />
      <br />
      <br />
      </label>

</form>
4

1 回答 1

1

Just replace this chunk of code, with my code below....should work...

     <?php
    //Print all the items in the shopping cart
    $totalAll = 0;
    foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {

    $totalAll = $totalAll + ($item['qty']*$item['price']);

    ?>
    <tr id="item<?php echo $itemNumber; ?>">    
        <td height="41"><a href="?remove=<?php echo $itemNumber; ?>">Remove Item</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="2" maxlength="3" /></td>
        <td>£<?php echo $item['qty'] * $item['price']; ?></td>  
        <td>£<?php echo $totalAll;?></td>
    </tr>
    <?php

    }
    ?>
于 2013-06-16T02:28:51.123 回答