desoto0311 Posted January 20, 2009 Share Posted January 20, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/141650-working-with-an-array-and-simple-shopping-cart/ Share on other sites More sharing options...
DeanWhitehouse Posted January 20, 2009 Share Posted January 20, 2009 For the second question you can use an if statement, quite simple. For the first question maybe use a multi-dimensional array, although this will require changing some of your pages which are using this array. Quote Link to comment https://forums.phpfreaks.com/topic/141650-working-with-an-array-and-simple-shopping-cart/#findComment-741458 Share on other sites More sharing options...
desoto0311 Posted January 20, 2009 Author Share Posted January 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/141650-working-with-an-array-and-simple-shopping-cart/#findComment-741541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.