rn14 Posted November 24, 2008 Share Posted November 24, 2008 This is where create a new object and pass values to it. I know it fills this with values as when i print out the values on this page they display. include('class.php'); $order = new Order(); $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist = $v; } } $order->Test($meallist); My problem occurs when i attempt to print these values out on a different page using the following code: <? include('class.php'); $array = $order->getData(); $meal1 = $array[0]; $meal2 = $array[1]; $meal3 = $array[2]; $meal4 = $array[3]; $meal5 = $array[4]; $meal6 = $array[5]; $meal7 = $array[6]; ?> I get this error: Fatal error: Call to a member function getData() on a non-object. Could anyone help? Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 Values in an object don't stay from page to page. Just like all other variables, the value of $order only exists for the lifetime of that script. To keep values from page to page, you need to store them in the SESSION Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 Thanks, if i then have a number of objects and put all of them into sessions is it possible to retrieve these on one page? Or will I have to insert the sessions into a db? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 You can save objects into the SESSION array, and then retrieve them on other pages just like you would any other variable. The only thing to remember is you need to load all of your classes (if you don't have an autoloader) before you call session_start(). Quote Link to comment Share on other sites More sharing options...
revraz Posted November 24, 2008 Share Posted November 24, 2008 Also know that if the browser is closed, the session info is destroyed. Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 if these are two objects that i have in sesssions how will I add them to the session array?? if these are two objects that i have in sessions below How will I add them to the session array? session_start(); include('class.php'); 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->Test($meallist); //changed this to reference index of 0. $_SESSION['order'] = serialize($order); ?> <? //order +1 if (isset($_SESSION['orderp2'])) { $orderp2 = unserialize($_SESSION['orderp2']); }else { $orderp2 = new Order(); } // incase we want to reset our test data. if ($_REQUEST['reset'] == "ok") { $orderp2 = new Order(); } $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal2") { $meallist = $v; } } $orderp2->Test($meallist); //changed this to reference index of 0. $_SESSION['orderp2'] = serialize($orderp2); ?> [code] Thanks Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 that is the code for 2 separate files right? <?php session_start(); include('class.php'); if (isset($_SESSION['order'])) { $order = $_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->Test($meallist); //changed this to reference index of 0. $_SESSION['order'] = $order; ?> <?php session_start(); include('class.php'); //order +1 if (isset($_SESSION['orderp2'])) { $orderp2 = $_SESSION['orderp2']; }else { $orderp2 = new Order(); } // incase we want to reset our test data. if ($_REQUEST['reset'] == "ok") { $orderp2 = new Order(); } $meallist = array(); foreach ($_REQUEST as $k=>$v) { if ($k=="meal2") { $meallist = $v; } } $orderp2->Test($meallist); //changed this to reference index of 0. $_SESSION['orderp2'] = $orderp2; ?> Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 They are both seperate and they are coming from different pages. Does this mean I will have to insert them into db?? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 no...would you like both scripts to use the same object? right now, one uses $_SESSION['order'] and the other uses $_SESSION['orderp2']. If you want them to share, have them use the same array key (aka, both use $_SESSION['order']) Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 Im fairly new to this so this might be stupid but can I put the two objects in one session? Did you mean use one one object but I amcollecting different data from multiple pages? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 the SESSION is shared across the entire visit for a particular user. So, when the user navigates from page to page, the info in SESSION can be written to/read from. If, on page1.php, you store a value into $_SESSION['order'], then on page2.php you can read the value from $_SESSION['order']. Does that make more sense? Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 Yes thanks, does that mean that if I have multiple objects of order they can all be retrieved from the one session called order? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted November 24, 2008 Share Posted November 24, 2008 If you want different instances of the order object, keep them in different parts of the session. $_SESSION['ordera'] = new Order(); $_SESSION['orderb'] = new Order(); $_SESSION['orderc'] = new Order(); Quote Link to comment Share on other sites More sharing options...
rn14 Posted November 24, 2008 Author Share Posted November 24, 2008 Thanks for your help here. Got this working now. 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.