Log in

View Full Version : [PHP] Session Help



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

Blob
18-10-2009, 05:18 PM
Delete this post please, read the code wrong.

Luno1599
19-10-2009, 01:26 PM
Also, How would you make the code check the session to check if they have already added the product and then update the quantity so it doesnt make a new session and then make duplicate sessions.

Dan

Protege
19-10-2009, 02:08 PM
for($i = 1; $i <= count($_SESSION['cart']); $i++) {

Luno1599
19-10-2009, 02:10 PM
for($i = 1; $i <= count($_SESSION['cart']); $i++) {


Thanks mate,

Also did you read my post above?

Dan

Luno1599
19-10-2009, 04:17 PM
Oh and how do I remove a product?

Dan

Luno1599
24-11-2009, 07:38 PM
Still need help on this please =)

Want to hide these adverts? Register an account for free!