rn14 Posted November 19, 2008 Share Posted November 19, 2008 Hi, Some background on what i am trying to do. Users can select a number of meals. When they have completed selection they are shown an order confirmation page. After viewing this page they can then view the rest of the site or make another purchase. if they make another purchase they will then be shown an order confirmation for this also. When they then hit the checkout button i want to be able to display all the orders they have made in one page. Thanks in advance for the help with my questions below. 1)Below I am attempting to call a function to return a value but to ensure that if the value isn't set that i dont get the following error: Fatal error: Call to a member function getID() on a non-object. It works but inconsistently for some reason. Is there a better way of doing this? $item1 = $order->getItemByIndex(0); if (isset($item1)){ $meal1 = $item1->getID(); } 2) In the code you will see i am returning data based on the meal id and i have a number of different meal ids. I use a separaate sql statement for each one and was wondering is there a better way of doing this? 3)With the code below I return all the values from a class. However i want to be able to insert each object which is allready in a session into a database but then will i be able to clear the information in each object so that i can use that object again to start a new order and still be able to retrieve the object that I have put in the database? Or would there be a better way of doing this? <? $order = unserialize($_SESSION['order']); // $array = array(); // $meal = array(); $items = $order->getItems(); $item1 = $order->getItemByIndex(0); if (isset($item1)){ $meal1 = $item1->getID(); } $item2 = $order->getItemByIndex(1); if (isset($item2)){ $meal2 = $item2->getID(); } $item3 = $order->getItemByIndex(2); if (isset($item3)){ $meal3 = $item3->getID(); } $item4 = $order->getItemByIndex(3); if (isset($item4)){ $meal4 = $item4->getID(); } $item5 = $order->getItemByIndex(4); if (isset($item5)){ $meal5 = $item5->getID(); } $item6 = $order->getItemByIndex(5); if (isset($item6)){ echo ""; } else{ $meal6 = $item6->getID(); } $item7 = $order->getItemByIndex(6); if (isset($item7)) echo ""; else $meal7 = $item7->getID(); ?> <div id="ourMenus"> <div id="orderProcessPages"> <h1 id="orderTitle">Order</h1> <div> <h1>Confirmation</h1> </div> <p> You have ordered the 4 WEEK PACKAGE with the following meals selected: </p> <div style="border: 0px dotted gray; width: 400px;"> <fieldset><legend>Week 1</legend> <ul> <? include('config.php'); $link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)or die("Could not connect: " . mysql_error()); mysql_select_db("rossnoon14_simp"); if (isset($meal1)){ $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal1 ;"); while($row = mysql_fetch_array($result)) { $image = $row['imagesmall']; $title = $row['title']; $description = $row['text']; } ?> <li><div style="float:left; padding-right: 10px;"><?echo $image."</div><br>"; echo $title;?></li> <div style="clear:left;"></div> <?}?> <? if (isset($meal2)){ $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal2 ;"); while($row = mysql_fetch_array($result)) { $image = $row['imagesmall']; $title = $row['title']; $description = $row['text']; } ?> <li><div style="float:left; padding-right: 10px;"><?echo $image."</div><br>"; echo $title;?></li> <div style="clear:left;"></div> <?}?> Quote Link to comment https://forums.phpfreaks.com/topic/133371-a-couple-of-questions/ Share on other sites More sharing options...
rn14 Posted November 20, 2008 Author Share Posted November 20, 2008 Can anybody help me with this? Have I explained the questions clearly? Quote Link to comment https://forums.phpfreaks.com/topic/133371-a-couple-of-questions/#findComment-693957 Share on other sites More sharing options...
Philip Posted November 20, 2008 Share Posted November 20, 2008 I'll give it a shot: 1. if (isset($item1))... will always happen, because you have initialized the variable $item1 in the statement above it. You need to check the return value, see if it's false or not. 2. Use the IN( ) command. $result = mysql_query("SELECT * FROM `menus_show` WHERE `id` IN('$meal1', '$meal2')"); That way, lets say if you have 3 as meal id #1, the query will show: SELECT * FROM `menus_show` WHERE `id` IN('3','') However, with using this, you need to make sure $meal1 and $meal2 are both sanitized, and initialized (if it's not, you can set it to ''). Also, this would combine the results, and I'm not sure if you wanted to keep it separated. Quote Link to comment https://forums.phpfreaks.com/topic/133371-a-couple-of-questions/#findComment-694011 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.