Jump to content

Problem


rn14

Recommended Posts

Hi,

 

I have a page where a user must select 5 meals but has seven options. Once meals are selected there id is saved to a class to be used ata later date. My problem occurs when I am trying to read the results from the class to my checkout page. Each result creates a new object of the class: shown here:

$item1 = $order->getItemByIndex(0);
$meal1 = $item1->getID();
$item2 = $order->getItemByIndex(1);
$meal2 = $item2->getID();
$item3 = $order->getItemByIndex(2);
$meal3 = $item3->getID();
$item4 = $order->getItemByIndex(3);
$meal4 = $item4->getID();
$item5 = $order->getItemByIndex(4);
$meal5 = $item5->getID();
$item6 = $order->getItemByIndex(5);
$meal6 = $item6->getID();
$item7 = $order->getItemByIndex(6);
$meal7 = $item7->getID();

 

When the user only selects five no ids are passed and no object will be created which reults in the following error:

 

Fatal error: Call to a member function getID() on a non-object

 

Which I understand but need some way of working around?

 

Anybody got any ideas?

 

Thanks in advance

Link to comment
Share on other sites

you can try this

 

<?php
$items = $_POST['items'];
$item = array();
$meal = array();

if(count($items) == 5){
foreach($items AS $itemz){
	if(is_int($itemz)){
		$item[$itemz] = $order->getItemByIndex($itemz);
		$meal[$itemz] = $item[$itemz]->getID();
	}
}

if(count($item) == 5){
	echo "GREAT SUCCESS";
}else {
	echo "something went terribly wrong...";
}
}else {
echo "you must select FIVE, not four, not six, but FIVE meals!";
}
?>

Link to comment
Share on other sites

This is my 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) {
if (is_array($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);
   }
}
?>

And this is how I tried to adapt the code above:

[code]
<?
$order = unserialize($_SESSION['order']);

$array = array();
$meal = array();


$items = $order->getItems();
if(count($items) == 5){// I tested at this point and the count was equal to five
foreach ($items as $item) {
if(is_int($item)){
$array[$item] = $order->getItemByIndex($item);
$meal[$item] = $array[$item]->getID();
}
}
if(count($array) == 5){
echo "GREAT SUCCESS";
}else {
echo "something went terribly wrong...";
}
}else {
echo "you must select FIVE, not four, not six, but FIVE meals!";
}

 

Any suggestions??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.