【发布时间】:2011-11-27 11:34:47
【问题描述】:
我正在尝试创建一个菜单,用户可以在其中查看他们创建的菜单。该菜单包含一个下拉框,用户可以使用该下拉框按周过滤菜单。
然后菜单会显示一周中的 7 天以及五种用餐时间,即早餐、午餐、晚餐、布丁和小吃。以下是我目前的展示方式。
$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>";
正如你所看到的,这个 PHP 语句从表中选择了我想要的,但是因为每周有 7 天和 5 种用餐时间,这意味着我必须复制这段代码 35 次才能显示我想要的所有内容想要。
所以例如下一段代码将是这样的:
$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>";
以此类推,直到我拥有所有的日子和他们的用餐时间。
有没有办法在不进行所有这些迭代的情况下显示所有信息?
【问题讨论】:
-
是的,您可以在数组中获得所需的所有信息。然后你开始输出。最好使用某种模板引擎。以 Smarty 为例。
-
ps。打印 - 比 echo 慢
-
@Userpassword 是的,我想,这将显着改善用户体验。一个不错的改进是使用 html 插值,并在与“查看”页面不同的部分进行查询。