2016-10-06 1 views
0

Hallo Ich möchte fragen, wie kann ich die Gesamtkosten eines Artikels haben. Ich habe Einkaufswagen und es zeigt nur Preismenge und Kosten für jeden Artikel. Ich möchte insgesamt Kosten aller Produkte haben, die helfen, durch den Benutzer hinzufügen worden bitteWie kann ich die Gesamtkosten aller Artikel, die vom Benutzer hinzugefügt wurden, haben?

das ist mein Warenkorb 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'])){ 
 
\t //Adding an Item 
 
\t //Store it in a Array 
 
\t $ITEM = array(
 
\t \t //Item name \t \t 
 
\t \t 'item_name' => $_GET['add'], 
 
\t \t //Item Price 
 
\t \t 'price' => $_GET['price'], 
 
\t \t //Qty wanted of item 
 
\t \t 'qty' => $_GET['qty'] \t \t 
 
\t \t); 
 

 
\t //Add this item to the shopping cart 
 
\t $_SESSION['SHOPPING_CART'][] = $ITEM; 
 
\t //Clear the URL variables 
 
\t 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'])){ 
 
\t //Remove the item from the cart 
 
\t unset($_SESSION['SHOPPING_CART'][$_GET['remove']]); 
 
\t //Re-organize the cart 
 
\t //array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART'])); 
 
\t //Clear the URL variables 
 
\t header('Location: ' . $_SERVER['PHP_SELF']); 
 

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

 
} 
 
else if (isset($_POST['update'])) { 
 
\t //Updates Qty for all items 
 
\t foreach ($_POST['items_qty'] as $itemID => $qty) { 
 
\t \t //If the Qty is "0" remove it from the cart 
 
\t \t if ($qty == 0) { 
 
\t \t \t //Remove it from the cart 
 
\t \t \t unset($_SESSION['SHOPPING_CART'][$itemID]); 
 
\t \t } 
 
\t \t else if($qty >= 1) { 
 
\t \t \t //Update to the new Qty 
 
\t \t \t $_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty; 
 
\t \t } 
 
\t } 
 
\t //Clear the POST variables 
 
\t 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 { 
 
\t width:650px; 
 
\t margin:auto; 
 
} 
 
#formArea #orderForm #formColumns #leftColumn { 
 
\t float:left; 
 
\t width:300px; 
 
} 
 
#orderForm { 
 
\t height:500px; 
 
} 
 
#formArea #orderForm #formColumns #rightColumn { 
 
\t float:right; 
 
\t width:300px; 
 
} 
 
#formArea #orderForm #formColumns th { 
 
\t text-align: left; 
 
} 
 
.copyright { 
 
\t 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> 
 
     \t \t \t 
 
\t \t \t 
 
     </tr> 
 
     <?php 
 
     } 
 
     ?> 
 
    </table> 
 
\t <?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>

Antwort

0

In diesem Fragment Sie einfach so etwas wie $total hinzufügen 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> 
+0

Verarbeitungsfehler: Syntaxfehler, unerwartet 'foreach' (T_FOREACH) in C: \ xampp \ htdocs \ Nike \ order.php auf Leitung 378 –

+0

Der Fehler ist hier foreach ($ _SESSION ['SHOPPING_CART'] als $ itemNumber => $ item) –

+0

sollte $ total = 0 sein; Ich vergaß über ';' Zeichen –

Verwandte Themen