rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 I have been trying to work this out. You come up with anything?? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688874 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Not sure, I just tested it using the above code and it worked just fine. The only thing I can think of is meallist is an array... do me a favor and run this: <?php session_start(); include('order.php'); // include the order class. if (isset($_SESSION['order'])) { $order = unserialize($_SESSION['order']); }else { $order = new Order(); } // incase we want to reset our test data. if ($_REQUEST['reset'] == "ok") { $order = new Order(); } $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } } echo "<pre>"; print_r($meallist); $order->addMultipleItems($meallist); $items = $order->getItems(); foreach ($items as $item) { echo $item->getID() . "<br />"; } $_SESSION['order'] = serialize($order); // now use $order as you wish $order->addItem(5); $item = $order->getItemByIndex(0); echo "Item ID of " . $item->getID() . " is at index of 0<br />"; echo "We currently have " . $order->getItemCount() . " items in your order.<br />"; ?> Post the results that come from that. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688879 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 It returns this: Array ( [0] => Array ( [0] => 28 [1] => 27 [2] => 26 [3] => 25 [4] => 24 [5] => 23 [6] => 22 ) ) Array Item ID of Array is at index of 0 We currently have 2 items in your order. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688882 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 So meallist is a multi dimensional array. try this. <?php session_start(); include('order.php'); // include the order class. if (isset($_SESSION['order'])) { $order = unserialize($_SESSION['order']); }else { $order = new Order(); } // incase we want to reset our test data. if ($_REQUEST['reset'] == "ok") { $order = new Order(); } $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } } $order->addMultipleItems($meallist[0]); //changed this to reference index of 0. $items = $order->getItems(); foreach ($items as $item) { echo $item->getID() . "<br />"; } $_SESSION['order'] = serialize($order); // now use $order as you wish $order->addItem(5); $item = $order->getItemByIndex(0); echo "Item ID of " . $item->getID() . " is at index of 0<br />"; echo "We currently have " . $order->getItemCount() . " items in your order.<br />"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688886 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 28 27 26 25 24 28 27 26 25 24 Item ID of 28 is at index of 0 We currently have 11 items in your order. It outputs the above. It is automatically incrementing orders by 5 now??? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688894 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 Its acually incrementing it by the number of meals in the array Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688896 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 It shouldnt increment the id's unless you tell it to Given your output from above: Array ( * => Array ( * => 28 [1] => 27 [2] => 26 [3] => 25 [4] => 24 [5] => 23 [6] => 22 ) ) Those are the values you are passing to the array...so this is working like expected. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688900 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 but its incrementing the number of items with the number of ids in the array. You get me? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688901 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Right thats how it should be...right? I do not see how one item can have 5 different ids....? This way you have a collection of items, this should replace the need to have multiple ids for an item, and just have one id. If that is not what is suppose to happen, than I am afraid I may have wasted alot of your time. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688927 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 There are seven ids for seven different things when this is selected it becomes one item. So basically i want to print the ids but to just increment the items by one? Is this a lot of changes?? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688934 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 No, that is not alot of changes. You have the right idea. Instead of having the class hold 5 different ids, it now can hold 5 different objects (which can be as many objects as you want) and you can just iterate (move) through the items or print them out like above instead of having to call 6 different function like before. I hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-688965 Share on other sites More sharing options...
rn14 Posted November 13, 2008 Author Share Posted November 13, 2008 How would I go about starting this?? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-689007 Share on other sites More sharing options...
premiso Posted November 13, 2008 Share Posted November 13, 2008 Oh sorry, my mind isnt paying attention today. With the new code added you should be able to increment each ID by one by doing the following: $order->incrementIDsByVal(1); Should increment all the IDs by 1. Change 1 to be whatever value you want and it will increment each item by that. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-689031 Share on other sites More sharing options...
rn14 Posted November 14, 2008 Author Share Posted November 14, 2008 But if I do that it increments the id that is at index of 0 not the actual number of id's that exist. What I want this entire program to do is the following: Have a meals class now called items class and allow this to take an array containing 5 ids each of which are meals. I then want to have an Item class that once a new array of 5 meals is added 1 is added to the total number of items. I have spent the day trying to understand your excelent piece of code but due to my lack of oop knowledge have struggled and instead tryed to revert back to the basics of understanding this. However I still need to get a solution to the above problem if you could help me that would be great. If you have time some explaination of the above code would also be great but I understand if you dont have time for any of this as you have helped a lot allready. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/page/2/#findComment-689865 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.