duanna1407 2016-10-06 15:08
浏览 34
已采纳

如何才能获得用户添加的所有项目的总成本? [关闭]

hi i want to ask on how can i have a total cost of an item. i have cart and it shows only price quantity and cost of each item. i want to have total of cost of all items that been add by the user please help

this is my cart code

<?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     
        'item_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']);
} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $_SERVER['HTTP_HOST']; ?> Online Order Form</title>
<style>


#formArea #orderForm #formColumns {
    width:650px;
    margin:auto;
}
#formArea #orderForm #formColumns #leftColumn {
    float:left;
    width:300px;
}
#orderForm {
    height:500px;
}
#formArea #orderForm #formColumns #rightColumn {
    float:right;
    width:300px;
}
#formArea #orderForm #formColumns th {
    text-align: left;
}
.copyright {
    font-size: 9pt;
}






</head>

<body>
    
        <?php 
        //Print all the items in the shopping cart
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td><a href="?remove=<?php echo $itemNumber; ?>">remove</a></td>
            <td><?php echo $item['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>
                
            
        </tr>
        <?php
        }
        ?>
    </table>
    <?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
    <p>
      <label>
      <input type="submit" name="update" id="update" value="Update Cart" />
      </label>
    </p>
</form>
<p><a href="javascript: history.go(-1)">Keep Shopping</a> - <a href="?empty">Empty</a> Cart</p>
</div>
<label>
     <form action="form.php">
    <input type="submit" id="checkout" value="Check Out" />
</form>
   
   
   
   
</body>
</html>

</div>
  • 写回答

1条回答

  • dongyixun0634 2016-10-06 15:21
    关注

    In this fragment you can simply add something like $total variable:

    <?php 
        //Print all the items in the shopping cart
        //## Declare your total value
        $total = 0;
    
        foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
            //## Count item price value
            $price_value = $item['qty'] * $item['price'];
            //## Sum prices:
            $total += $price_value;
        ?>
        <tr id="item<?php echo $itemNumber; ?>">    
            <td><a href="?remove=<?php echo $itemNumber; ?>">remove</a></td>
            <td><?php echo $item['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 $price_value ?></td><tr>
        <?php
        } 
        ?>
        <tr>
             <td colspan="4">Sum:</td>
             <td> <?php echo $total; //## echo your total ?> </td>
        </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿