Jump to content

Object Question


rn14

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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 />";
?>


Link to comment
Share on other sites

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);
      }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
   }
}
?>

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.