chrometweaks.org

iPage Quick Shopping Cart Template?

Click Here To View All Answers...


My question is iPage Quick Shopping Cart Template? Hoping for any comment. Another question... 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..

Comments (68)

Good question... I dunno what is the right answer. I'll do some research and get back to you if I bump into an decent answer. You should email the people at iPage as they probably could assist you..

Comment #1

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..

Comment #2

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...

Comment #3

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!..

Comment #4

I didnt take it out I made it the array index so there is no need to duplicate $prodID in the array contents..

$_SESSION['cart'][$prodID] =..

Comment #5

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...

Comment #6

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..

Comment #7

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

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

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

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

This post has been edited by.

Project_aism.

: 20 March 2004, 23:44..

Comment #8


This question was taken from a support group/message board and re-posted here so others can learn from it.