Jump to content

comparing associative array session data


metazai

Recommended Posts

Still working on a shopping cart -- I'm able to add new items easily, using the unique product ID, then an array of "entries" beneath that to account for items being entered with different options (3 red shirts, 2 blue shirts, etc -- same product ID) . This works fine:

<?php $_SESSION['cart'][$prodid][] = array ( 
                 'option1' => $opt1, 
                 'option2' => $opt2, 
     'option3' => $opt3, 
                 'quantity' => $qty 
                );
?>

However, is there a simple way to read associative array session data so that if someone enters, say, the same product they just ordered with the same options I can catch that and just add to the quantity in the ID-Entry-Quantity Associative Array? (so if someone enters 3 red shirts, then orders 2 blue shirts, then goes back and adds 3 more red shirts it doesn't show up like:

 

3 red shirts

2 blue shirts

3 red shirts

 

but rather:

 

6 red shirts

2 blue shirts

 

Here's my ugly way, doesn't work:

<?php session_start();
$product_id=$_POST['product_id'];
$product_quantity=$_POST['product_quantity'];
$variable_option1=$_POST['variable_option1'];
$variable_option2=$_POST['variable_option2'];
$variable_option3=$_POST['variable_option3'];
  
add_to_cart ($product_id,$variable_option1,$variable_option2,$variable_option3,$product_quantity); 
function  add_to_cart ($prodid, $opt1, $opt2, $opt3, $qty) 
{ 
$i=0; 
// check ENTRY arrays under Product ID array 
  foreach($_SESSION['cart'][$prodid] as $decoy => $prodentry) { 
// check options for each array 
if  (($_SESSION['cart'][$prodid][$prodentry]['option1'] == $opt1) && ($_SESSION['cart'][$prodid][$prodentry]['option2'] == $opt2) && ($_SESSION['cart'][$prodid][$prodentry]['option3'] == $opt3)) { $_SESSION['cart'][$prodid][$prodentry]['quantity']=($_SESSION['cart'][$prodid][$prodentry]['quantity'] + $qty); $i=1; }}  
if ($i=0){ 
    $_SESSION['cart'][$prodid][] = array ( 
                 'option1' => $opt1, 
                 'option2' => $opt2, 
     'option3' => $opt3, 
                 'quantity' => $qty 
                );} 
} 
?> 

A friend suggested array_diff() but I don't see how to work it.  I'm getting an error message as it is, so I think I'm passing the wrong type of data.  And I plan to give customers the options to change the amounts in the cart itself, but I want to cover all my bases.

 

Any thoughts?

the version of the function I gave you worked for me

function  add_to_cart ($prodid, $opt1, $opt2, $opt3, $qty)
{
    $found=0;
    if (isset($_SESSION['cart'][$prodid]))
    {
        foreach ($_SESSION['cart'][$prodid] as $i => $order)
        {
            if ($order['option1']==$opt1 && $order['option2']==$opt2 && $order['option3']==$opt3)
            {
                $_SESSION['cart'][$prodid][$i]['quantity'] += $qty;
                $found=1;
                break;
            }
        }
    }
    
    if (!$found) 
    {
        $_SESSION['cart'][$prodid][] = array (
                 'option1' => $opt1,
                 'option2' => $opt2,
                 'option3' => $opt3,
                 'quantity' => $qty
                );
    }
}

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.