Julian Posted October 25, 2007 Share Posted October 25, 2007 Hi guys Here's my problem... I have a table called food (id_food | food) and a table called restaurants (id_restaurant | name | id_food) I'm storing the id_food as an array in table restaurants like this (1,5,8,10) What I want to do is show the results on restaurant page that show me the array on id_food in table restaurants. But not show the id numbers but the food name stored on table food. Thanks for the great help!!! Quote Link to comment https://forums.phpfreaks.com/topic/74766-help-with-an-array/ Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 Given the vague description without a real coding example you want to use the foreach loop www.php.net/foreach Whether it needs to be nested or not I am un-sure, but you also want the food table to be in it's own associative array, or store the name of the food with the id in the restaurants table. If you store just the ID in the resturaunts table and the food has it's own master array that is associative by the id, you can simply call $food[$id] which should print out the name, given the food array structure is like: $food = array(1 => "Pizza", 34 => "Candy"); Then $food[1] would print "Pizza". If you want better help, paste current code with the array structures. Quote Link to comment https://forums.phpfreaks.com/topic/74766-help-with-an-array/#findComment-377988 Share on other sites More sharing options...
Julian Posted October 25, 2007 Author Share Posted October 25, 2007 Here's I have so far: <?php //explode the array $dbtypes = $row_edit['id_food']; $db_array = explode(',', $dbtypes); $sql = "SELECT id_food, food FROM food ORDER BY food"; $res = mysql_query($sql) or die(mysql_error()); echo '<table width="100%" border="0" cellspacing="0" cellpadding="3">'; $count = 0; while (list($id_food, $food) = mysql_fetch_row($res)) { if ($count % 4 == 0) echo '<tr>'; echo "<td>$food</td>"; $count++; if ($count % 4 == 0) echo '</tr>'; } // if we got to the end but haven't closed the row if ($count % 4 != 0) echo '</tr>'; echo '</table>'; ?> If you see the page result: http://www.costaricaconcierge.com/users/restaurantes_detail.php?dia=25&mes=10&ano=2007&id_local=90 I just want to show the array id_food stores on table restaurant. $food should be the array stored on restaurant.id_food Hope this clarify my question. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/74766-help-with-an-array/#findComment-378023 Share on other sites More sharing options...
sasa Posted October 25, 2007 Share Posted October 25, 2007 change line echo "<td>$food</td>"; to if (in_array($id_food, $db_array)) echo "<td>$food</td>"; Quote Link to comment https://forums.phpfreaks.com/topic/74766-help-with-an-array/#findComment-378121 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.