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

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>
php

1个回答

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>
dpkpaxhzffixp8426
dpkpaxhzffixp8426 当然,您可以将这些项目保存到db。 如果你想使用普通的PHP,你可以使用mysqli或PDO。 如果我对$ total的回答是正确的,请将其标记为已接受。
接近 4 年之前 回复
dongshang1529
dongshang1529 我将再次提出另一个问题,那些保存到数据库的项目是否可能? 我没有输入所有项目到数据库。 我想只使用插入。 我的意思是仅供预订
接近 4 年之前 回复
dongwen7730
dongwen7730 谢谢你的工作
接近 4 年之前 回复
drqrdkfue521903877
drqrdkfue521903877 应该是$ total = 0; 我忘记了; 标志
接近 4 年之前 回复
dongqian9013
dongqian9013 错误在这里foreach($ _SESSION ['SHOPPING_CART']为$ itemNumber => $ item)
接近 4 年之前 回复
drkjzk3359
drkjzk3359 解析错误:语法错误,第378行的C:\ xampp \ htdocs \ Nike \ order.php中的意外“foreach”(T_FOREACH)
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐