Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Session Array


qwerty123

Recommended Posts

Hi,

I have two simple questions on session arrays. I have created a session array for a shopping cart see below for the code [1].

When a user clicks on a url it adds the data to the session array. The problem is I want it to only add unique products (i.e. ones which have not been added to the cart beforehand, products which already have been added to the cart I want to just add one to ?qty? and increase the price of the specific productId so we don?t have duplicates in the session array). The problem is I?m not sure how to this you can see the code which I have written so far [2] to do this. Please could help me just add the remaining code?

 

[1]
$_SESSION['cart'][] = array(
       "qty" => $qty,
       "productId" => $prodID,
       "price" => $prodprice,
       "prodName" => $prodname
);

 

[2]
//check that the product we are adding has not been added beforehand
if(isset($_SESSION['cart']))
{
foreach($_SESSION['cart'] as $cartItems)
{
 //If we have the product already
 if ($cartItems['productId'] == $prodID)
 {
                   // ****Code needed to be added here***********
                  // Change the product details in the session array (cart)                     // by adding 1 to quantity (qty) and change the total                      // price of the specific product	
	 
 }

 // if we don?t have duplicates add the product to the session 
                               //array
 else
 {
     $_SESSION['cart'][] = array(                                      "qty" => $qty,
     "productId" => $prodID,
     "price" => $prodprice,
     "prodName" => $prodname
                                               );

 }
}
}

//if the session array has not been created beforehand create cart session array
else
{
$_SESSION['cart'][] = array(                                  "qty" => $qty,
              "productId" => $prodID,                  "price" => $prodprice,
              "prodName" => $prodname
    );
}

 

 

Also if I needed to delete a specific product i.e. a product with productId=1, in a the session array ($_SESSION['cart'][]), how would I do that?

 

Thank-you for your help!

 

Kind Regards

John A

Link to comment
Share on other sites

You've designed the array as like a database table.

 

Use the following instead, this moves the key to a more manageable level -

 

$_SESSION['cart'][$prodId]

 

Delete and search by using isset() and unset().

 

There is no need to do a sequential search with foreach. Thats overkill and very slow.

Link to comment
Share on other sites

I'm not really sure what you mean could you explain a bit more please?

Can't I implement like i've done?

$_SESSION['cart'][] = array(

"qty" => $qty,

"productId" => $prodID,

"price" => $prodprice,

"prodName" => $prodname

);

 

Thank-you

Link to comment
Share on other sites

Instead of this

$_SESSION['cart'][0] = array(
"qty" => $qty,
"productId" => $prodID,
"price" => $prodprice,
"prodName" => $prodname
);

 

You need this

$_SESSION['cart'][$prodID] = array(
"qty" => $qty,
"price" => $prodprice,
"prodName" => $prodname
);

 

Then you simply do this when it exists.

$_SESSION['cart'][$prodID]['qty'] +=1;

 

The problem with your method above is that $cartItems is a copy of one item from $_SESSION['cart'] so if you do $cartItems['qty']+=1 the value in $_SESSION['cart'] [unknownindex]['qty'] will not be changed.

Link to comment
Share on other sites

Hi, user99999999

Let me confirm this you say I should do it like this:

 

$_SESSION['cart'][$prodID] = array(
"qty" => $qty,
"price" => $prodprice,
"prodName" => $prodname
);

 

But I also have to set the productId in the session array and you seem to have taken it out?

 

$_SESSION['cart'][] = array(                                     
"qty" => $qty,
"productId" => $prodID,
"price" => $prodprice,
"prodName" => $prodname
);

 

So if I had to loop through the new session array how would it be constructed thanks and if I wanted to delete a product (I know I use unset) but how do you precisely delete just one value i.e. with productId = 1.

 

 

Thank-you

 

Kind Regards!

Link to comment
Share on other sites

So if I want print the value of the session array cart, I do the following

foreach($_SESSION['cart'] as $cartItems)
{

echo $cartItems[?prodID?];
echo "<p>";
echo $cartItems['qty'];
echo "<p>";
echo $cartItems['price'];
echo "<p>";
echo $cartItems['prodName'];
echo "<p>";

}

However nothing appears however when I uncomment the line: echo $cartItems[?prodID?];

it prints the values, but how do I print the ProductID associated with each product.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...