Jump to content

Session help


teejayy1013

Recommended Posts

Hi everyone,

 

I'm trying to create a shopping-cart esque style system and I'm having a bit of trouble.

 

Currently I have the shopping cart displaying the sku and the quantity associated with that sku. I have it adding the values so if you go back and add the same item to your cart again, it updates the quantity from 1 to 2, not just adding another entry.

 

However, now I want it to display the price associated with that item and I'm getting a little confused. Currently, whenever I add something to the cart, it updates ALL the prices in the cart to the price of the newly added item.

 

For example: if I have one ipod sock in there for 12.00 and then add ipod headphones for 30.00, it says "1 ipod sock 30.00, 1 ipod headphones 30.00" I dont know what I'm doing wrong. I included the code below.

 

Current Order:<br> 
<? 
    if(isset($_GET['submittedFormTest'])){ 
        $sku= $_GET['sku']; 
        $quantity = $_GET['quantity']; 
        $price=$_GET['price']; 
        $_SESSION[$sku] = $_SESSION[$sku] + $quantity; 
?> 
        <table border=1> 
            <tr><td>Quantity</td><td>sku</td><td>Description</td><td>Price Each</td><td>Total</td><tr> 
<?                 
        foreach($_SESSION as $key=>$value){ 
            echo "<tr><td>$key</td><td>$value</td><td></td><td>$price</td><td></td></tr> <br />"; 
        }     
    } 
?>  

 

 

I've tried adding a variable $_SESSION[$price] but then the foreach explodes that SESSION variable and I can't figure out how to make that for each statement only apply to the $_SESSION[$sku];

Link to comment
https://forums.phpfreaks.com/topic/100988-session-help/
Share on other sites

You could make each session sku variable into an object:

(You would need to keep a list of the sku's)

 

Current Order:<br> 
<? 
    if(isset($_GET['submittedFormTest'])){ 
        $sku= $_GET['sku']; 
        $quantity = $_GET['quantity']; 
        $price=$_GET['price']; 

        if(!in_array($sku, $_SESSION['sku_list'])){
                $_SESSION['sku_list'][] = $sku;
        }
        $_SESSION[$sku]['quantity'] += $quantity; 
        $_SESSION[$sku]['price'] = $price;
?> 
        <table border=1> 
            <tr><td>Quantity</td><td>sku</td><td>Description</td><td>Price Each</td><td>Total</td><tr> 
<?                 
        foreach($_SESSION['sku_list'] as $sku){ 

            echo "<tr><td>$sku</td><td>$_SESSION[$sku]['quantity']</td><td></td><td>$_SESSION[$sku]['price']</td><td></td></tr> <br />"; 
        }     
    } 
?>  

Link to comment
https://forums.phpfreaks.com/topic/100988-session-help/#findComment-516499
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.