rn14 Posted November 10, 2008 Share Posted November 10, 2008 I am trying to return the values that are stored in this class at least i think they are: This is my order class: <? class order{ var $id; var $id1; var $id2; var $id3; var $id4; var $id5; var $id6; public function _construct ( $or_id, $or_id1, $or_id2, $or_id3, $or_id4, $or_id5, $or_id6 ) { $this->id = $or_id; $this->id1 = $or_id1; $this->id2 = $or_id2; $this->id3 = $or_id3; $this->id4 = $or_id4; $this->id5 = $or_id5; $this->id6 = $or_id6; } public function getid(){return $id;} public function getid1(){return $id1;} public function getid2(){return $id2;} public function getid3(){return $id3;} public function getid4(){return $id4;} public function getid5(){return $id5;} public function getid6(){return $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;} } ?> This is where I fill the class with information <? 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[1][0]; $id2 = $meallist[2][0]; $id3 = $meallist[3][0]; $id4 = $meallist[4][0]; $id5 = $meallist[5][0]; $id6 = $meallist[6][0]; echo "<br>"; echo $id; echo "<br>"; $class = new order($id,$id1,$id2,$id3,$id4,$id5,$id6); $_SESSION['class'] = serialize($class); ?> This is where i try and return these values: <? session_start(); include('test.php'); $class = unserialize($_SESSION['class']); $class = new order('id','id1','id2','id3','id4','id5','id6'); echo $class ->getid()->id; echo $class ->getid1()->id1; ?> It just returns nothing and I am certain those values are filled Thanks in advance for any help Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 $class = unserialize($_SESSION['class']); $class = new order('id','id1','id2','id3','id4','id5','id6'); It looks like you are just overwriting $class with new values here. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 and this: public function getid(){return $id;} public function getid1(){return $id1;} public function getid2(){return $id2;} public function getid3(){return $id3;} public function getid4(){return $id4;} public function getid5(){return $id5;} public function getid6(){return $id6;} needs to be this: 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;} Quote Link to comment Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 No sure on this, but would you not just call the get id like so: <?php session_start(); include('test.php'); $class = unserialize($_SESSION['class']); // $class = new order('id','id1','id2','id3','id4','id5','id6'); Commented out do to overwriting. echo $class->getid(); echo $class->getid1(); ?> I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos? Editied thanks to flyhoney for correcting. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 No sure on this, but would you not just call the get id like so: <?php session_start(); include('test.php'); $class = unserialize($_SESSION['class']); // $class = new order('id','id1','id2','id3','id4','id5','id6'); Commented out do to overwriting. echo $class->getid; echo $class->getid1; ?> I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos? That is how you access class variables, not class functions. class Foo { $var = 'test'; function bar () { echo 'baz'; } } $foo = new Foo(); echo $foo->var; // prints 'test' $foo->bar(); // prints 'baz' Quote Link to comment Share on other sites More sharing options...
premiso Posted November 10, 2008 Share Posted November 10, 2008 No sure on this, but would you not just call the get id like so: <?php session_start(); include('test.php'); $class = unserialize($_SESSION['class']); // $class = new order('id','id1','id2','id3','id4','id5','id6'); Commented out do to overwriting. echo $class->getid; echo $class->getid1; ?> I am not 100% sure, but I think that is how you should access them? Also are you not overwriting class with the line above the echos? That is how you access class variables, not class functions. class Foo { $var = 'test'; function bar () { echo 'baz'; } } $foo = new Foo(); echo $foo->var; // prints 'test' $foo->bar(); // prints 'baz' Yea, I meant to keep the parans there and just remove the ->id; My mistake on that part, thanks for correcting. Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 Wow I didnt even notice that: echo $class ->getid()->id; echo $class ->getid1()->id1; needs to be echo $class ->getid(); echo $class ->getid1(); Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 Thanks for the replies. i have made those changes and i still cant get it to work. any other ideas??? Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 Should the variables at the start of the order class by defined with $var instead of var??? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 Check out the modifications I made: I am trying to return the values that are stored in this class at least i think they are: This is my order class: <?php class order{ var $id; var $id1; var $id2; var $id3; var $id4; var $id5; var $id6; public function _construct($or_id, $or_id1, $or_id2, $or_id3, $or_id4, $or_id5, $or_id6) { $this->id = $or_id; $this->id1 = $or_id1; $this->id2 = $or_id2; $this->id3 = $or_id3; $this->id4 = $or_id4; $this->id5 = $or_id5; $this->id6 = $or_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;} 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;} } ?> This is where I fill the class with information <? 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[1][0]; $id2 = $meallist[2][0]; $id3 = $meallist[3][0]; $id4 = $meallist[4][0]; $id5 = $meallist[5][0]; $id6 = $meallist[6][0]; echo "<br>"; echo $id; echo "<br>"; $class = new order($id,$id1,$id2,$id3,$id4,$id5,$id6); $_SESSION['class'] = serialize($class); ?> This is where i try and return these values: <? session_start(); include('test.php'); $class = unserialize($_SESSION['class']); $class = new order('id','id1','id2','id3','id4','id5','id6'); echo $class->getid(); echo $class->getid1(); ?> It just returns nothing and I am certain those values are filled Thanks in advance for any help Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 still no good i have been looking at this for yours and can not seem to work out what it is. Thanks again. Any other suggestions?? Quote Link to comment Share on other sites More sharing options...
trq Posted November 10, 2008 Share Posted November 10, 2008 Your overiding $class with another instance. <? session_start(); include('test.php'); $class = unserialize($_SESSION['class']); $class = new order('id','id1','id2','id3','id4','id5','id6'); echo $class->getid(); echo $class->getid1(); ?> Should be.... <?php session_start(); include('test.php'); $class = unserialize($_SESSION['class']); echo $class->getid(); echo $class->getid1(); ?> ps: This is a particularly poorly designed piece of code. What happens if you need to add more items than the class can currently handle? You'd need to modify the class. Quote Link to comment Share on other sites More sharing options...
trq Posted November 10, 2008 Share Posted November 10, 2008 Also, _construct needs to be __construct. Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 I have taken out that piece that overrides and still cant get it to work. I am fairly new to this so apologies for the bad code. I would like to know how to form this better and for it to be able to handle more items. if you could even point me in the right direction here that would be great? Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 it now works but only returns the first id and not the others??? When I add this echo $class ->getid1(); echo $class ->getid2(); echo $class ->getid3(); echo $class ->getid4(); echo $class ->getid5(); echo $class ->getid6(); Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 I would break it up into two classes: <?php class Order { private $items = array(); function __construct() { } function addItem($item) { $this->items[] = $item; } function getItems() { return $this->items; } } class Item { private $id; function __construct($id) { $this-id = $id; } function setId($id) { $this->id = $id; } function getId() { return $this->id; } } ?> And you would use it like this: <?php $order = new Order(); $item = new Item(100); $order->addItem($item); $items = $order->getItems(); foreach ($items as $item) { echo $item->getId(); } ?> Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 This is a stupid question but how will I add the id's to the array?? Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 I need each id number will i be able to get that by doing like this?? Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 10, 2008 Author Share Posted November 10, 2008 Im really trying to understand this but would really appreciate a little bit more help. I need to return each of those id's for every item would it be neccessary to have an array for id's aswell? Will I still use a session just as I have?? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted November 10, 2008 Share Posted November 10, 2008 I'm sorry, I think I may have tried to throw too much at you. You probably want to read more on classes to get a better feel for exactly how to use them. You're original code, with the modifications we mentioned, will work, it just may not be optimal. If I were you, I would start using a lot of echo statements, and see what happening at every point in the script, so you can narrow things down to which line of code is giving you trouble. Quote Link to comment 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.