Luno1599
18-10-2009, 03:00 PM
Hey,
I have this code:
<?php
session_start();
if($_GET['del'] == "yes"){
session_destroy();
}
function addToCart() {
// count at what order_line number we currently are and increment with 1 for our next line
$order_line_id = count($_SESSION['cart']) + 1;
$_SESSION['cart'][$order_line_id]['product_id'] = $_POST['product_id']; // unique product id, should be taken from your DB
$_SESSION['cart'][$order_line_id]['quan'] = $_POST['quan'];
$_SESSION['cart'][$order_line_id]['price'] = $_POST['price']; // price for each product
}
if(isset($_POST['submit'])) { // did the user press a submit button?
addToCart(); // run the addToCart function to add the form information to the session array.
}
if($_SESSION['cart']){
$total_order_price = 0;
for($i = 0; $i <= count($_SESSION['cart']); $i++) {
echo "Product id : " . $_SESSION['cart'][$i]['product_id'] . "<br>";
echo "Quantity : " . $_SESSION['cart'][$i]['quan'] . "<br>";
echo "Price : " . $_SESSION['cart'][$i]['price'] . "<br>";
$line_price = $_SESSION['cart'][$i]['price'] * $_SESSION['cart'][$i]['quan'];
echo "Total price : " . $line_price . "<br><br>";
$total_order_price = $total_order_price + $line_price;
}
echo "Shipping: 4.99<br><br>";
$total_order_price_ship = $total_order_price + 4.99;
echo "Totale order price : ". $total_order_price_ship;
}else{
echo "No Cart!";
}
?>
<form action="test2.php" method="post">
<input name="product_id" type="hidden" id="product_id" value="IMG-001">
<input name="quan" type="hidden" id="quan" value="5">
<input name="price" type="hidden" id="price" value="1.00">
<input type="submit" name="submit" value="Add">
</form>
And when you click add, it shows this:
Product id :
Quantity :
Price :
Total price : 0
Product id : IMG-001
Quantity : 5
Price : 1.00
Total price : 5
Shipping: 4.99
Totale order price : 9.99
Why does it make one with nothing in it?!
It only does it with the first one...
Dan
I have this code:
<?php
session_start();
if($_GET['del'] == "yes"){
session_destroy();
}
function addToCart() {
// count at what order_line number we currently are and increment with 1 for our next line
$order_line_id = count($_SESSION['cart']) + 1;
$_SESSION['cart'][$order_line_id]['product_id'] = $_POST['product_id']; // unique product id, should be taken from your DB
$_SESSION['cart'][$order_line_id]['quan'] = $_POST['quan'];
$_SESSION['cart'][$order_line_id]['price'] = $_POST['price']; // price for each product
}
if(isset($_POST['submit'])) { // did the user press a submit button?
addToCart(); // run the addToCart function to add the form information to the session array.
}
if($_SESSION['cart']){
$total_order_price = 0;
for($i = 0; $i <= count($_SESSION['cart']); $i++) {
echo "Product id : " . $_SESSION['cart'][$i]['product_id'] . "<br>";
echo "Quantity : " . $_SESSION['cart'][$i]['quan'] . "<br>";
echo "Price : " . $_SESSION['cart'][$i]['price'] . "<br>";
$line_price = $_SESSION['cart'][$i]['price'] * $_SESSION['cart'][$i]['quan'];
echo "Total price : " . $line_price . "<br><br>";
$total_order_price = $total_order_price + $line_price;
}
echo "Shipping: 4.99<br><br>";
$total_order_price_ship = $total_order_price + 4.99;
echo "Totale order price : ". $total_order_price_ship;
}else{
echo "No Cart!";
}
?>
<form action="test2.php" method="post">
<input name="product_id" type="hidden" id="product_id" value="IMG-001">
<input name="quan" type="hidden" id="quan" value="5">
<input name="price" type="hidden" id="price" value="1.00">
<input type="submit" name="submit" value="Add">
</form>
And when you click add, it shows this:
Product id :
Quantity :
Price :
Total price : 0
Product id : IMG-001
Quantity : 5
Price : 1.00
Total price : 5
Shipping: 4.99
Totale order price : 9.99
Why does it make one with nothing in it?!
It only does it with the first one...
Dan