rn14 Posted November 12, 2008 Share Posted November 12, 2008 I have this class <?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); } } ?> This populates them: <?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(); } $number = $_POST["number"]; echo $number; $val=$number; echo $val; $order->incrementIDsByVal($val); $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 />"; ?> This is displayed Array Item ID of Array is at index of 0 We currently have 2 items in your order. The word "Array" should be an id anybody willing to take a look at this Quote Link to comment https://forums.phpfreaks.com/topic/132491-help-please/ Share on other sites More sharing options...
premiso Posted November 12, 2008 Share Posted November 12, 2008 Not sure why you created a new topic here, but I posted a reply already on the original thread. Quote Link to comment https://forums.phpfreaks.com/topic/132491-help-please/#findComment-688884 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.