Jump to content

Working with an Array and Simple Shopping Cart-


desoto0311

Recommended Posts

Hey all-

 

I've got a simple array I'm using for a small shopping cart system. Here's the basic array:

$cart(1,3,3,1,1,); // '1' equals the 'main' product that I need to work with. This would read as 3 product "1's", and 2 product "3's".

The cart is being saved in a session.

 

I need this modified in two ways:

1.) I basically need a product 'option' value that can be added/recorded (possibly in a second array?) for the product '1', above. Other products have no additional 'option'.

2.) My cart currently loops with a foreach, counts as quantity and displays contents. I'd need to adjust this so that the product '1' would not display quantities, but actually display a line for each iteration of '1', and show the 'option' below it. The other product would display as one line and show quantity.

 

Example:

prod1 (option1) $price

prod1 (option3) $price

prod1 (option1) $price

prod3 QUANTITY $price

 

Is this too complicated for an array? I'd go with a DB (much preferred) but so much of the other pages are dependent on the session's $cart array, and the main $pro Product array that I'd think it'd be a nightmare to modify.

 

Any suggestions would be helpful. I've included a text file and listed all pertinent pages. (Note that I already have code for Product 1 to change the price of it after Quantity greater than 1. That would need to stay).

 

Thanks in advance.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I'm really not familiar with multi-dimensional arrays. I'll play with the code though.

 

Also, as far as an 'if' statement, I'm pretty familiar with using said statements in php, but when working with foreach and arrays, etc. I'm quite lost. I can't seem to figure out, for example, how to put an if inside of a foreach statement.

 

Example:

foreach ($contents as $id=>$qty) {

global $pros;

extract($pros[$id]);

 

//This is checks if it's item one, and if so, changes pricing structure

if ($id == 1) {//

$newprice = $price + $price2*($qty - 1);

$pricePrint = number_format($newprice, 2, '.', '');

} else {

$pricePrint = number_format(($price * $qty), 2, '.', '');

//echo "ThisistheID else ".$id;

}

// output:

$output[] = "html".$vars here;

}

This isn't perfect, just took a 'piece' of the existing code. So right NOW, it takes the contents and counts them, displays only ONE iteration of the cart item, then adds a quantity area which is $qty. This would need to somehow have an 'if' above all of this I would assume that says 'if' 1, then echo each iteration (and somehow get the product 'option' information to echo out) and if not 1, then go with the code here.

 

It's a little beyond me though.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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