Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

iterate object PHP 4.0

Status
Not open for further replies.

CassidyHunt

IS-IT--Management
Jan 7, 2004
688
US
I am testing out a theory and want to know why I get it to echo the $i variable but not the $obj variable in my foreach loop. It loops through with the 6 positions of the array just does not print the value.

Am I doing something wrong?

Code:
<?php
class shoppingcart
{
	var $item_index;
	var $cart_item;

	function additem($qty,$productcode,$description,$weight,$price)
	{
		if(!isset($this->item_index))	{ $this->item_index = 0; }
			else{ $this->item_index++; }
		
		$cart_item[$this->item_index] = new item;

		$cart_item[$this->item_index]->qty = $qty;
		$cart_item[$this->item_index]->productcode = $productcode;
		$cart_item[$this->item_index]->description = $description;
		$cart_item[$this->item_index]->weight = $weight;
		$cart_item[$this->item_index]->price = $price;
		$cart_item[$this->item_index]->status = "Active";
		
		echo $cart_item[$this->item_index]->productcode . "<BR>";
		return false;
		
		
	}

	function removeitem($index)
	{
		$this->cart_item[$index]->status = "Deleted";
		return false;
	}

	function getitem($index)
	{
		$item = array($this->cart_item[$index]->qty,
			      $this->cart_item[$index]->productcode,
			      $this->cart_item[$index]->description,
			      $this->cart_item[$index]->weight,
			      $this->cart_item[$index]->price,
			      $this->cart_item[$index]->status);
		
		return $item;
		
	}

}

class item
{
	var $qty;
	var $productcode;
	var $description;
	var $weight;
	var $price;
	var $status;
}

?>

<?php
$myobj = new shoppingcart;
$myobj->additem('3','A100','Magical Widget #1','3lbs','6.00');
$myobj->additem('2','A101','Magical Widget #2','2lbs','4.00');
$myobj->additem('1','A102','Magical Widget #3','1lbs','2.00');

for($i = 0; $i < 3; $i++ ) {
$myitem = $myobj->getitem($i);

foreach($myitem as $obj){ echo $i . ".  " . $obj . "<br>"; }
}

echo "complete";
?>

Thanks

Cassidy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top