mpar612 Posted August 6, 2008 Share Posted August 6, 2008 Hi there, I am new to the forums and have worked with PHP on and off for the last two years. I apologize for the length of the post. I am creating a small shopping cart. I want the user to be able to maneuver from page to page and remember what items are in their cart. Every time the user adds an item I load all of their selections into an array, $_SESSION['order']. Then on each page I print all of the contents that are in the array into hidden form fields on the page as long as the items in the array are not items that are listed on the current page that the user is viewing. I currently use a foreach() loop to query the database of products (from the 'items' table) and compare it against the values in $_SESSION['order']. I then print out any values that are not listed as items on the particular page that the user is viewing as hidden form fields. Now, for the items that are in the array that are a part of the page the user is viewing I want to populate the form fields on the product page with the values that are in the array. I am having a problem with the nested foreach() loops in my code. I have tried many different combinations of foreach() loops to make this work. Does anyone have any suggestions on how to make this work? I hope my description makes sense. If you need any clarification, please let me know. Any guidance or tips in any way would be greatly appreciated. Thanks in advance. global $db; $categorySql = "SELECT * FROM menuCategories WHERE menu = '$menu'"; $categoryRows = $db->getAll($categorySql); $itemsSql = "SELECT * FROM items WHERE menu = '$menu'"; $itemsRows = $db->getAll($itemsSql); foreach ($_SESSION['order'] as $key => $value) { $found = false; foreach ($itemsRows as $itemsRow) { if ($itemsRow['itemId'] == $key) { $found = true; break; } } if (!$found) { echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />"; } } foreach ($categoryRows as $categoryRow) { $category = $categoryRow['category']; $categoryMenu = $categoryRow['menu']; $itemsSql = "SELECT * FROM items WHERE menu = '$categoryMenu' AND category = '$category'"; $itemsRows = $db->getAll($itemsSql); echo "<table border=\"0\" width=\"541\" id=\"table1\"> <tr> <td colspan=\"3\" height=\"34\" valign=\"top\"><b><font face=\"Times New Roman\" color=\"#990000\">$category:</font></b></td> </tr>"; foreach ($itemsRows as $itemsRow) { $itemId = $itemsRow['itemId']; $itemName = $itemsRow['itemName']; $temperature = $itemsRow['temperature']; $itemDescription = $itemsRow['itemDescription']; $price = $itemsRow['price']; echo "<tr> <td width=\"24\" valign=\"top\" align=\"center\"><input type=\"text\" size=\"3\" name=\"$itemId\"></td> <td width=\"421\" valign=\"top\"><span style=\"font-family: Times New Roman\"><font size=\"2\" color=\"#3F1F08\">$itemName</font></span></td>"; if($itemDescription != ""){ echo "<td width=\"421\" valign=\"top\"> <span style=\"font-family: Times New Roman\"> <font size=\"2\" color=\"#3F1F08\">$itemDescription</font></span></td>"; } echo "<td width=\"82\" align=\"right\" valign=\"top\"><font face=\"Times New Roman\" size=\"2\" color=\"#3F1F08\">$$price</font></td> </tr>"; } echo "</table>"; } Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/ Share on other sites More sharing options...
mbeals Posted August 6, 2008 Share Posted August 6, 2008 you could seriously simplify your code with simple array functions which will get rid of some of the nesting and make the code easier to read: ie: <?php foreach ($_SESSION['order'] as $key => $value) { $found = false; foreach ($itemsRows as $itemsRow) { if ($itemsRow['itemId'] == $key) { $found = true; break; } } if (!$found) { echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />"; } } ?> becomes <?php foreach ($itemsRows as $itemsRow) { if (array_key_exists ($itemsRow['itemId'], $_SESSION['order'])) { echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" />"; } } ?> peruse this list http://us2.php.net/array other then that, it's hard to help without more info on exactly what is breaking Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-609662 Share on other sites More sharing options...
mpar612 Posted August 6, 2008 Author Share Posted August 6, 2008 Thanks for the response. I will work with that. My only question now is how do I get the values out of the array and into the hidden input field? My code had "foreach ($_SESSION['order'] as $key => $value)" but that is not required using array_key_exists. How do I still extract these values? Thanks! Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-609781 Share on other sites More sharing options...
mpar612 Posted August 7, 2008 Author Share Posted August 7, 2008 bump Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610302 Share on other sites More sharing options...
mpar612 Posted August 7, 2008 Author Share Posted August 7, 2008 bump Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610663 Share on other sites More sharing options...
mbeals Posted August 7, 2008 Share Posted August 7, 2008 Sorry, Can you give me an example of the $_SESSION and $itemRows arrays? I just want to see their structure. Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610686 Share on other sites More sharing options...
mpar612 Posted August 7, 2008 Author Share Posted August 7, 2008 The $_SESSION['order'] array values will look something like 2=>12, 3=>13. It is an item number that is auto-incremented by MySQL and a quantity entered into a form by the user. It is in Item Number => Quantity format. The $itemRows is coming from a database. It contains the following fields: itemId, itemName, itemDescription and itemPrice. The itemId would be any integer from 1 to 100. the itemName and itemDescription are text and the itemPrice is a number. Does this give you the information you need? Thanks! Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610761 Share on other sites More sharing options...
mbeals Posted August 7, 2008 Share Posted August 7, 2008 is $itemRows an array of associative arrays? Like: array ( [0] = array( 'itemID' => ' ' , 'itemName' => ' ', .....) [1] = array( 'itemID' => ' ' , 'itemName' => ' ', .....) [2] = array( 'itemID' => ' ' , 'itemName' => ' ', .....) ) Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610862 Share on other sites More sharing options...
mbeals Posted August 7, 2008 Share Posted August 7, 2008 assuming that structure (which I think is correct looking back at your original code, this should do it: <?php foreach($_SESSION['order'] as $key=>$value){ foreach($itemsRows as $row){ if($row['itemId'] != $key){ echo "<input type=\"hidden\" name=\"$key\" value=\"$value\" /><br>"; break; } } } ?> Link to comment https://forums.phpfreaks.com/topic/118440-arrays-db-and-nested-foreach-loops/#findComment-610894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.