I am trying to create a menu where users can view a menu that they create. The menu consists of a dropdown box which the users use to filter the menu by week.
The menu then displays the seven days of the week and the five types of meal times their are which is breakfast, lunch, dinner, pudding and snack. Below is how i am currentley displaying it so far.
$sqlmeasurement = mysql_query("SELECT title FROM recipe JOIN menu ON recipe.recipeid = menu.recipeid WHERE menu.weekid = '".$selectweek."' AND menu.mealtimeid = '1'") or die(mysql_error());
Print '<table border="0">
<tr>
<th width="100"> </th>
<th width="200">Monday</th>
</tr>';
echo "<tr>";
echo "<td><strong>Breakfast</strong></td>";
echo "<td>";
while($info = mysql_fetch_array( $sqlmeasurement ))
{
echo $info['title'];
echo "<br/>";
}
echo "</td> ";
echo "</tr>";
echo "</table>";
As you can see this PHP statement selects what i want from the table but because there are 7 days a week and 5 types of meal times this means that i will have to copy this code 35 times in order to display all that i want.
So for example the next bit of code will be like this:
$sqlmeasurement2 = mysql_query("SELECT title FROM recipe JOIN menu ON recipe.recipeid = menu.recipeid WHERE menu.weekid = '".$selectweek."' AND menu.mealtimeid = '2'") or die(mysql_error());
Print '<table border="0">';
Print '<tr>
<th width="100"> </th>
<th width="200"> </th>
</tr>';
echo "<tr>";
echo "<td><strong>Lunch</strong></td>";
echo "<td>";
while($info2 = mysql_fetch_array( $sqlmeasurement2 ))
{
echo $info2['title'];
echo "<br/>";
}
echo "</td> ";
echo "</tr>";
echo "</table>";
And so on until i have all the days and their meal times.
Is there a way to display all the information without all this iteration?
You have various options. You could put the output into a function, and just call that with varying parameters.
Or you could use a different query to get all the information at once, and loop through it:
You can then iterate through the results, assembling them into, say, a 2D array based on using mealtimename as a key. e.g.