PHP有更简单、更整洁的方法吗?

时间:2021-08-11 18:03:15

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.

然后,菜单显示一周的七天,以及五种用餐时间,即早餐、午餐、晚餐、布丁和零食。下面是我到目前为止如何显示它的currentley。

 $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.

正如您看到的,这个PHP语句从表中选择了我想要的东西,但是因为每周有7天和5种用餐时间,这意味着我必须复制这个代码35次,以便显示我想要的所有东西。

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?

是否有一种方法可以在不进行所有迭代的情况下显示所有信息?

1 个解决方案

#1


3  

You have various options. You could put the output into a function, and just call that with varying parameters.

你有很多不同的选择。你可以把输出放到一个函数中,然后用不同的参数调用它。

function ouptutMeal($selectweek, $mealtime, $mealname) {
    $sqlmeasurement2 = mysql_query("SELECT ... WHERE menu.weekid = '$selectweek' AND menu.mealtimeid = '$mealtime'");

    echo "<table>
            <tr>
              <th> </th>
              <th> </th>
           </tr>
           <tr>
             <td><strong>$mealname</strong></td>
             <td>";
    while($info2 = mysql_fetch_array( $sqlmeasurement2 )) { 
        echo $info2['title'], '<br/>';
    } 
    echo    '</td>
           </tr>
         </table>';
}

ouptutMeal($selectweek, 1, 'breakfast');
ouptutMeal($selectweek, 2, 'lunch');

Or you could use a different query to get all the information at once, and loop through it:

或者你也可以使用不同的查询一次获取所有的信息,然后循环使用:

$q = "SELECT title, mealtimeid, mealtimename 
      FROM ... 
      WHERE menu.weekid = '$selectweek' 
      ORDER BY menu.mealtimeid";

You can then iterate through the results, assembling them into, say, a 2D array based on using mealtimename as a key. e.g.

然后,您可以遍历结果,将它们组合成,比方说,基于使用mealtimename作为键的2D数组。如。

$mealInfo = array()

while($info = mysql_fetch_array( $result )) { 
    $mealInfo[$info['mealtimename'][] = $info;
} 

foreach ($mealInfo as $mealName => $mealData) {
    ouptutMeal($mealName, $mealData);
}

#1


3  

You have various options. You could put the output into a function, and just call that with varying parameters.

你有很多不同的选择。你可以把输出放到一个函数中,然后用不同的参数调用它。

function ouptutMeal($selectweek, $mealtime, $mealname) {
    $sqlmeasurement2 = mysql_query("SELECT ... WHERE menu.weekid = '$selectweek' AND menu.mealtimeid = '$mealtime'");

    echo "<table>
            <tr>
              <th> </th>
              <th> </th>
           </tr>
           <tr>
             <td><strong>$mealname</strong></td>
             <td>";
    while($info2 = mysql_fetch_array( $sqlmeasurement2 )) { 
        echo $info2['title'], '<br/>';
    } 
    echo    '</td>
           </tr>
         </table>';
}

ouptutMeal($selectweek, 1, 'breakfast');
ouptutMeal($selectweek, 2, 'lunch');

Or you could use a different query to get all the information at once, and loop through it:

或者你也可以使用不同的查询一次获取所有的信息,然后循环使用:

$q = "SELECT title, mealtimeid, mealtimename 
      FROM ... 
      WHERE menu.weekid = '$selectweek' 
      ORDER BY menu.mealtimeid";

You can then iterate through the results, assembling them into, say, a 2D array based on using mealtimename as a key. e.g.

然后,您可以遍历结果,将它们组合成,比方说,基于使用mealtimename作为键的2D数组。如。

$mealInfo = array()

while($info = mysql_fetch_array( $result )) { 
    $mealInfo[$info['mealtimename'][] = $info;
} 

foreach ($mealInfo as $mealName => $mealData) {
    ouptutMeal($mealName, $mealData);
}