jolyon_russ Posted June 10, 2007 Share Posted June 10, 2007 I'm a Flash developer by trade and use OOP on a daily basis so understand how things should work. I'm hoping my problem is just syntactical. Anyway, I'm building the simplest shopping cart, all it has to do is store the products temporarily, they will be passed out to Nochex's payment completion system, then dumped. So, I have my page with an add to cart button. It kicks off with creating basket_arr, then on _POST creating an instance of itemVO, which in tern is passed to an instance of basket <?php session_start ( ) ; if ( isset ( $_SESSION['basket_arr'] ) ) { print 'basket exists ' . $_SESSION['basket_arr'] ; } else { $_SESSION['basket_arr'] = array ( ) ; } if(isset($_POST['add'])) { $id = $_POST['id'] ; $price = $_POST['price'] ; $number = $_POST['number'] ; require ( 'vo/itemVO.php' ) ; $item = new itemVO ( ) ; $item -> id = $id ; $item -> price = $price ; $item -> number = $number ; require ( 'basket.php' ) ; $basket = new basket ( ) ; $basket -> addItem ( $item ) ; header("Location: " . $_SERVER['PHP_SELF']); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>Untitled Document</title> </head> <body> <form method="post"> <p>Carrots - £6.99 - <input name="id" type="hidden" value="carrots" /> <input name="price" type="hidden" value="1.50" /> <input name="number" type="text" value="1" size="3" maxlength="3" /> <input name="add" type="submit" class="box" id="carrots" value="Add To Basket"> </p> </form> </body> </html> I have my itemVO, simple enough: <?php class itemVO { var $id ; var $price ; var $number ; function UserVO() { } } ?> Now the area that I'm having my trouble with is my basket.php, the plan is to have my session hold an array of itemVO's. Obviously if I submit the same item more than once I need to check that it exists, if it does, add it to the existing one if not add the new itemVO to the array. <?php class basket { function addItem ( $item ) { $len = count ( $_SESSION['basket_arr'] ) ; if ( $len > 0 ) { for ( $i = 0 ; $i < $len ; $i++ ) { //loop through basket array if ( $_SESSION['basket_arr'][$i]->id == $item->id ) { //if the id of the session array is the same as the submitted item, add it echo ( 'the item trying to be added already exists' ) ; $_SESSION['basket_arr'][$i]->number += $item->number ; } else { echo ( 'there is no match add the new itemVO' ) ; array_merge ( $_SESSION['basket_arr'] , $itemVO ) ; } } } else { //basket_arr has no length so go ahead and add it $_SESSION['basket_arr'][0] = $item ; } } } ?> I hope this doesn't come across as another, "why doesn't this work" post, and more of a, "I know this should be working, any pointers as to why is might not be". I know that I could navigate to basket.php passing the itemVO as a query and add it that way, but think I'll probably just have the same problem. Ultimately I want to be able to add items from the product page without navigating away. Thanks for all your help in advance. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/ Share on other sites More sharing options...
paul2463 Posted June 10, 2007 Share Posted June 10, 2007 Hello just a quick look through your code for basket, one problem could be that when you have not found the item in the basket you array_merge() the item, but you are passing it "$itemVO" instead of "$item" which you pass to the function addItem($item) regards Paul Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-271784 Share on other sites More sharing options...
emehrkay Posted June 10, 2007 Share Posted June 10, 2007 the one thing that you have to do when storing class instances in session vars is to include/require the class before you do your session_start() call (i'm not sure if you did, by looking at that first block) are you getting any errors when you run that basket class? if not, try adding error_reporting(E_ALL); to the top of your script and see what is happening Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-271919 Share on other sites More sharing options...
jolyon_russ Posted June 11, 2007 Author Share Posted June 11, 2007 Cheers Paul, you're right I was trying to pass in $itemVO, this error appeared while I was formatting the code for the post. Emehrkay, thanks for the include/require tip, I'll give that a try and report back, hopefully it should do the trick. I did have error reporting on, but it was throwing a weird include-ish error (can't remember it off the top of my head hence the rather lame explaination) but maybe your suggestion will sort this out. Thanks a lot both. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-272215 Share on other sites More sharing options...
jolyon_russ Posted June 12, 2007 Author Share Posted June 12, 2007 It's working, thanks for the help, including the files before the session_start() worked and since then I've written a getBasketItem method which gets called from my checkout page. I may be back for more questions but in the mean time I'm cracking on with it. Cheers again. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-272976 Share on other sites More sharing options...
paul2463 Posted June 12, 2007 Share Posted June 12, 2007 if its working to your happiness can you please click the SOLVED button to prevent others needing to look at this post to see if something needs solving and not just for interest - many thanks Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-272997 Share on other sites More sharing options...
dStreSd Posted June 12, 2007 Share Posted June 12, 2007 Also one quick tip, generally when you want to store objects (or most types of complicated data) in something like a session or database it's better to serialize the data (serialize) and unserialize it when you need it (unserialize()) Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-273495 Share on other sites More sharing options...
jolyon_russ Posted June 14, 2007 Author Share Posted June 14, 2007 I thought I'd got it working, but after building it up to multiple pages passing items to the session array it seems to break. By break I mean it adds extra items to the array when it should be adding to the number. I can add as many of 1 item as I want, but when I add a different item more than once I get this: Array ( [0] => itemVO Object ( [id] => carrots [price] => 1.50 [number] => 10 ) [1] => itemVO Object ( [id] => peas [price] => 2.50 [number] => 2 ) [2] => itemVO Object ( [id] => peas [price] => 2.50 [number] => 1 ) ) I've attached a zip with all the files, if anyone has 5 mins to take a look at these I would really appreciate it. Cheers. Jolyon [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-274693 Share on other sites More sharing options...
emehrkay Posted June 14, 2007 Share Posted June 14, 2007 i dont understand what is wrong, you have an array of objects right? isnt that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-275005 Share on other sites More sharing options...
jolyon_russ Posted June 18, 2007 Author Share Posted June 18, 2007 Hey emehrkay, Yeah, it's what I want but I need them to be unique objects. If the object already exists it add the value to the existing one. The problem I'm having is that it's adding the same object twice and more. The array I pasted in shows 3 objects their id's are "carrots", "peas" and "peas" when it should just be "carrots" and "peas". Obviously these are hypothetical items for sale on my website. Is there any reason why this line would actually add a new item to the array? $_SESSION['basket_arr'][$i]->number += $item->number ; Thanks in advance. Jolyon Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-276631 Share on other sites More sharing options...
emehrkay Posted June 18, 2007 Share Posted June 18, 2007 are you sure your if statement isnt falling through to the else? thats what it looks like. maybe you should try to add somethign in your if to let you know that it is actually passing im talking aobut this block here: <?php if ( $_SESSION['basket_arr'][$i]->id == $item->id ) { //if the id of the session array is the same as the submitted item, add it echo ( 'the item trying to be added already exists' ) ; $_SESSION['basket_arr'][$i]->number += $item->number ; } else { echo ( 'there is no match add the new itemVO' ) ; array_merge ( $_SESSION['basket_arr'] , $itemVO ) ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54945-objects-stored-in-sessions/#findComment-276789 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.