rn14 Posted November 12, 2008 Share Posted November 12, 2008 Hi, I use the following to store information in a class and then retrieve it at a latter stage. However what i want to do is when I have stored the first piece of information in the class for the user to be able to return and submit another piece of information(an order in this case) and for that to displayed with the initial piece of information at a later stage. The class object is stored in a session. This are the classes i use <?php class Item{ private $id; private $id1; private $id2; private $id3; private $id4; private $id5; private $id6; public function __construct($id, $id1, $id2, $id3, $id4, $id5, $id6) { $this->id = $id; $this->id1 = $id1; $this->id2 = $id2; $this->id3 = $id3; $this->id4 = $id4; $this->id5 = $id5; $this->id6 = $id6; } public function setid($id){$this->id = $id;} public function setid1($id1){$this->id1 = $id1;} public function setid2($id2){$this->id2 = $id2;} public function setid3($id3){$this->id3 = $id3;} public function setid4($id4){$this->id4 = $id4;} public function setid5($id5){$this->id5 = $id5;} public function setid6($id6){$this->id6 = $id6;} public function getid(){return $this->id;} public function getid1(){return $this->id1;} public function getid2(){return $this->id2;} public function getid3(){return $this->id3;} public function getid4(){return $this->id4;} public function getid5(){return $this->id5;} public function getid6(){return $this->id6;} } class Order { private $items = array(); function __construct() { } function addItem($item) { $this->items[] = $item; } function getItems() { return $this->items; } } ?> This is how I add data to the class: <? include('test.php'); session_start(); $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } } print_r($meallist); echo "<br>"; $result = count($meallist, COUNT_RECURSIVE); echo $result; echo "<br>"; $id = $meallist[0][0]; $id1 = $meallist[0][1]; $id2 = $meallist[0][2]; $id3 = $meallist[0][3]; $id4 = $meallist[0][4]; $id5 = $meallist[0][5]; $id6 = $meallist[0][6]; $order = new Order(100); $item = new Item($id,$id1,$id2,$id3,$id4,$id5,$id6); $order->addItem($item); $items = $order->getItems(); foreach ($items as $item) { echo $item->getid() . "<br>"; echo $item->getid1() . "<br>"; echo $item->getid2() . "<br>"; echo $item->getid3() . "<br>"; echo $item->getid4() . "<br>"; echo $item->getid5() . "<br>"; echo $item->getid6() . "<br>"; } $_SESSION['item'] = serialize($item); $_SESSION['order'] = serialize($order); ?> This is how I retrieve it <? include('test.php'); session_start(); $item = unserialize($_SESSION['item']); $order = unserialize($_SESSION['order']); $items = $order->getItems(); foreach ($items as $item) { echo $item->getid() . "<br>"; echo $item->getid1(); $id = $item->getid2() . "<br>"; echo $item->getid3() . "<br>"; echo $item->getid4() . "<br>"; echo $item->getid5() . "<br>"; echo $item->getid6() . "<br>"; echo $id; } ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/ Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Honestly, with that code you are defeating the purpose of classes and objects. Here is a revised version of your code with examples: usage.php: <?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(0 => array(1, 2, 3, 4)); $order->addMultipleItems($meallist[0]); $items = $order->getItems(); foreach ($items as $item) { echo $item->getID() . "<br />"; } // 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 />"; $_SESSION['order'] = serialize($order); // add at the end incase of any changes they all get caught. ?> order.php <?php class Item { private $id; public function __construct($id) { $this->id = $id; } public function setID($id) { $this->id = $id; } public function getID() { return $this->id; } } class Items { private $itemArray = array(); // optional parameter to take in an array of ids or a single ID public function __construct($ids = "") { if ($ids != "" && is_array($ids)) { $this->addMultipleItems($ids); }elseif ($ids != "") { $this->addItem($ids); } } // adds an item. public function addItem($itemID) { // use the count to set the index for the item. $this->itemArray[count($this->itemArray)] = new Item($itemID); return true; } // returns all items public function getItems() { return $this->itemArray; } // returns an item object -1 = no item in the array at the index public function getItemByIndex($index) { return (isset($this->itemArray[$index]))?$this->itemArray[$index]:-1; } // returns the item requested by id. public function getItemByID($id) { if (is_array($this->itemArray)) { foreach ($this->itemArray as $item) { if ($item->getID() == $id) return $item; } } return -1; } public function getIndexOfItemByID($id) { if (is_array($this->itemArray)) { foreach ($this->itemArray as $key => $item) { if ($item->getID() == $id) return $key; } } return -1; } public function getItemCount() { return count($this->itemArray); } // takes in an array of IDs and creates each item. public function addMultipleItems($ids) { foreach ($ids as $val) { $this->itemArray[$this->getItemCount()] = new Item($val); } } public function setItemIDByIndex($index, $newID) { return $this->itemArray[$index]->setID($newID); } public function setItemIDByIID($oldID, $newID) { $index = getIndexOfItemByID($oldID); if ($index != -1) return $this->itemArray[$index]->setID($newID); return false; } } class Order { private $items; // takes in a set of ids or a single id, either way our // items constructor will handle it. function __construct($ids="") { $this->items = new Items($ids); } public function addItem($id) { $this->items->addItem($id); } public function addMultipleItems($ids) { $this->items->addMultipleItems($ids); } // returns an item for the given index, ie 0, 1, 2, 3, 4 public function getItemByIndex($index) { return $this->items->getItemByIndex($index); } public function getItemCount() { return $this->items->getItemCount(); } public function setItemIDByIndex($index, $newID) { return $this->items->setItemIDByIndex($index, $newID); } public function setItemIDByID($oldID, $newID) { return $this->items->setItemIDByID($oldID, $newID); } public function getItemByID($id) { return $this->items->getItemByID($id); } function getItems() { return $this->items->getItems(); } } ?> Now the class is not fully tested, but with the examples given it has been tested and seems to work. If you need help explaining that let me know. Using this concept you should be able to have as many items as you want and should be able to loop through each item with ease. Anyhow I was just really bored so yea. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688716 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 I hope you are fairly bored I am going to a bit of explaining done! I get the following error: Fatal error: Call to a member function addMultipleItems() on a non-object Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688745 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 What code are you using to get that error? The code should run fine as long as it is included and copied like it should be... Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688749 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 is it because the array is not being filled with ids? <p><input type="checkbox" name="meal[]" value="<?php echo $row['id']; ?>" I pass the id to the usage page using the above Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688756 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 The error that was given indicates that the class was not included in the page via include('class.php'); because it is saying that there is no function called addMultipleItems(). Make sure you saved the class as a separate file and included that file. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688757 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 I had copied the code directly with the include ('order.php'); I called the everything the same as what you had. When i remove the include I get a different error. Any suggestions Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688764 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 When i print the value of $meallist it returns as not containing any values Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688766 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Post the code where you are defining $meallist. It is hard to help without code =) Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688768 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 Apologies, Here it is $meallist = array(); echo $meallist; $order->addMultipleItems($meallist[0]); $items = $order->getItems(); Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688771 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 You arent assigning meallist any value. If you are posting to the page this adding this back in: $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } } That way you will get your data once again =) Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688772 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 It now prints the values of the array but still get the same error. Thanks for your patience. Anything else it could be? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688778 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Post the full code of the page you are working with. As stated I doubt you are including the class file, which would be the exact reason why. Without the class definition the page has no clue what you are talking about. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688783 Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 @premiso: No, that's not what the error indicates. It means that he tried to use a non-object as an object. Like this: $somevar = "testing"; $somevar->lol(); Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688784 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 @premiso: No, that's not what the error indicates. It means that he tried to use a non-object as an object. Like this: $somevar = "testing"; $somevar->lol(); Ah didnt even think of that. Where are you setting $order to the class? I guess either way the full code would help out tremendously. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688788 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 This is the function // takes in an array of IDs and creates each item. public function addMultipleItems($ids) { foreach ($ids as $val) { $this->itemArray[$this->getItemCount()] = new Item($val); } This is usage.php <?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; } } print_r($meallist); $order->addMultipleItems($meallist[1]); $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/#findComment-688789 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Add the ?reset=ok to the end of the usage.php so its like this: usage.php?reset=ok to reset the session object. What it seems like is the session is storing the old object order and not creating a new one. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688790 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 That fixed that thanks. But there is a new issue Warning: Invalid argument supplied for foreach() // takes in an array of IDs and creates each item. public function addMultipleItems($ids) { foreach ($ids as $val) { $this->itemArray[$this->getItemCount()] = new Item($val); } Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688797 Share on other sites More sharing options...
DarkWater Posted November 12, 2008 Share Posted November 12, 2008 Change: $order->addMultipleItems($meallist[1]); To: $order->addMultipleItems($meallist); Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688801 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist[] = $v; } } print_r($meallist); $order->addMultipleItems($meallist); // change this to be just meallist (I do not think it should be multi-dimensional) To avoid this error in the future we can code this in: // takes in an array of IDs and creates each item. public function addMultipleItems($ids) { if (is_array($ids)) { foreach ($ids as $val) { $this->itemArray[$this->getItemCount()] = new Item($val); } } } That way it only processes if the item passed is an array. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688803 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 Thanks very much premiso your help is massively appreciated. Thanks darkwater also. Could i you guys some more questions??? I will just mention them anyway and if ye have time please help. Firstly if I also had an option for the user to increment the number of orders they make themselves how could also let this variable update the number of items in the order. Secondly if I have another page of meals and I also want users to be able to store these meals in a class would i be able to re-use this class? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688812 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 First question: I am not understanding it. Are you asking that you want to change an oldID to a new one or increment all the ids in the order by 1? If the later a new function would need to be coded to loop through the items array and invoke the setID to equal getID + 1. Second question: If you name the object $meals = new order(); there should not be any type of issue with having multiple objects at all. For the code for the first add this inside the order class: public function incrementIDsByVal($val) { return $this->items->incrementIDsByVal($val); } Next add this to the Items class public function incrementIDsByVal($val) { if (!is_numeric($val) && $val < 1) { return false; }else { foreach ($this->itemArray as $key => $item) { $this->itemArray[$key]->setID(($item->getID() + $val)); } } } Untested so prone to errors of syntax but should work. Let me know if that was not what you were talking about. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688826 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 A quick question for you premiso this is the output that is displayed: Array Array Array Item ID of Array is at index of 0 We currently have 4 items in your order. Should array not be id numbers? Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688853 Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Post the code you are using please. Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688855 Share on other sites More sharing options...
rn14 Posted November 12, 2008 Author Share Posted November 12, 2008 usage.php <?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); $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 />"; ?> order.php <?php class Item { private $id; public function __construct($id) { $this->id = $id; } public function setID($id) { $this->id = $id; } public function getID() { return $this->id; } } class Items { private $itemArray = array(); // optional parameter to take in an array of ids or a single ID public function __construct($ids = "") { if ($ids != "" && is_array($ids)) { $this->addMultipleItems($ids); }elseif ($ids != "") { $this->addItem($ids); } } // adds an item. public function addItem($itemID) { // use the count to set the index for the item. $this->itemArray[count($this->itemArray)] = new Item($itemID); return true; } // returns all items public function getItems() { return $this->itemArray; } // returns an item object -1 = no item in the array at the index public function getItemByIndex($index) { return (isset($this->itemArray[$index]))?$this->itemArray[$index]:-1; } // returns the item requested by id. public function getItemByID($id) { if (is_array($this->itemArray)) { foreach ($this->itemArray as $item) { if ($item->getID() == $id) return $item; } } return -1; } public function getIndexOfItemByID($id) { if (is_array($this->itemArray)) { foreach ($this->itemArray as $key => $item) { if ($item->getID() == $id) return $key; } } return -1; } public function getItemCount() { return count($this->itemArray); } // takes in an array of IDs and creates each item. public function addMultipleItems($ids) { foreach ($ids as $val) { $this->itemArray[$this->getItemCount()] = new Item($val); } } public function setItemIDByIndex($index, $newID) { return $this->itemArray[$index]->setID($newID); } public function setItemIDByIID($oldID, $newID) { $index = getIndexOfItemByID($oldID); if ($index != -1) return $this->itemArray[$index]->setID($newID); return false; } public function incrementIDsByVal($val) { if (!is_numeric($val) && $val < 1) { return false; }else { foreach ($this->itemArray as $key => $item) { $this->itemArray[$key]->setID(($item->getID() + $val)); } } } } class Order { private $items; // takes in a set of ids or a single id, either way our // items constructor will handle it. function __construct($ids="") { $this->items = new Items($ids); } public function addItem($id) { $this->items->addItem($id); } public function addMultipleItems($ids) { $this->items->addMultipleItems($ids); } // returns an item for the given index, ie 0, 1, 2, 3, 4 public function getItemByIndex($index) { return $this->items->getItemByIndex($index); } public function getItemCount() { return $this->items->getItemCount(); } public function setItemIDByIndex($index, $newID) { return $this->items->setItemIDByIndex($index, $newID); } public function setItemIDByID($oldID, $newID) { return $this->items->setItemIDByID($oldID, $newID); } public function getItemByID($id) { return $this->items->getItemByID($id); } function getItems() { return $this->items->getItems(); } public function incrementIDsByVal($val) { return $this->items->incrementIDsByVal($val); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132458-object-question/#findComment-688856 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.