循环中的PHP循环 - 尝试对具有匹配ID的项进行分组

时间:2017-10-06 12:39:14

标签: php mysqli

对不起,如果这看起来像一个愚蠢的问题,但我是这种方法的新手。我所拥有的是一系列与用餐相对应的餐食。目前,我的代码通过meal_id将两个表链接在一起,并在循环中运行所选记录。我不喜欢的是给每个餐食项目提供相关的膳食行记录,如果它们对应于相同的膳食ID则应该分组。我已经通过$result启动了查询。我只是无法将餐食作为一餐进行分组。

你可以做的任何事情都很棒!

数据库返回如下:

mysqli_result Object
(
    [current_field] => 0
    [field_count] => 11
    [lengths] => 
    [num_rows] => 140
    [type] => 0
)

这是我的代码......

<?php
    while($row = mysqli_fetch_assoc($result))
{
    ?>
<tr>

<td align='center'><?php echo($row['meal_id']);?></td>
<td align='center'><?php echo($row['user_id']);?></td>
<td align='center'><?php echo(date("d/m/Y", strtotime($row['meal_date'])));?></td>
<td align='center'><?php echo(date("g:i A", strtotime($row['meal_time'])));?></td>
<td align='center'><?php echo($row['meal_notes']);?></td>

</tr>

我想在每个餐食项下面循环下面<tr>,然后继续下一餐

    <tr><td align='center'><?php echo($row['meal_item_name']);?></td>
<td align='center'><?php echo($row['meal_item_measure']);?></td>
<td align='center'><?php echo($row['meal_item_measurement']);?></td>
</tr>
<?php
}
?>

目前我得到:

   |meal_id  user_id  |  meal_date  |  meal_time  |  meal_notes  |
    |meal_item_name  |  meal_item_measure | meal_item_measurement  |
    |meal_id  user_id  |  meal_date  |  meal_time  |  meal_notes  |
    |meal_item_name  |  meal_item_measure | meal_item_measurement  |
    |meal_id  user_id  |  meal_date  |  meal_time  |  meal_notes  |
    |meal_item_name  |  meal_item_measure | meal_item_measurement  |
    |meal_id  user_id  |  meal_date  |  meal_time  |  meal_notes  |
    |meal_item_name  |  meal_item_measure | meal_item_measurement  |
    |meal_id  user_id  |  meal_date  |  meal_time  |  meal_notes  |
    |meal_item_name  |  meal_item_measure | meal_item_measurement  |

但我想得到它

|meal_id  |  user_id  |  meal_date  |  meal_time  |  meal_notes  |
|meal_item_name   |  meal_item_measure  | meal_item_measurement  |
|meal_item_name   |  meal_item_measure  | meal_item_measurement  |
|meal_item_name   |  meal_item_measure  | meal_item_measurement  |

|meal_id |   user_id  |  meal_date  |  meal_time  |  meal_notes  |
|meal_item_name   |  meal_item_measure |  meal_item_measurement  |

|meal_id  |  user_id  |  meal_date  |  meal_time  |  meal_notes  |
|meal_item_name   |  meal_item_measure | meal_item_measurement   |
|meal_item_name   |  meal_item_measure | meal_item_measurement   |

等: - )

3 个答案:

答案 0 :(得分:2)

我相信您正在尝试创建一个多维数组,其中meal_id将是每行的ID,并且用餐本身将存储在该ID中。

首先,需要更多设置。你应该稍微改变你的查询。

您的查询需要按meal_id列进行排序。例如ORDER BY meal_id ASC

完成此操作后,在当前while语句之前,您应该遍历您的值并将它们放入具有正确格式的自己的数组中。

//Create an array to store our grouped rows
$grouped = array();

//Loop over all rows returned by the $result that has been executed.
foreach($result as $value){

    //shift ID off table, so it doesn't get stored again in the grouped arrays (Except for the array key).
    $id = array_shift($value);

    //gather initial row table information, and unset so it isn't stored for each row.
    $user_id = $value['user_id']; unset($value['user_id'];
    $meal_date = $value['meal_date']; unset($value['meal_date'];
    $meal_time = $value['user_id']; unset($value['meal_time'];
    $meal_notes = $value['meal_notes']; unset($value['meal_notes'];

    //Check if the array key doesn't exist, if it doesn't then we add it.
    //This allows us to make a multidimensional array where each ID is a meal_id
    if(!array_key_exists($id, $grouped)){
        $grouped[$id] = array(array("user_id" => $user_id, "meal_date" => $meal_date, "meal_time" => $meal_time, "meal_notes" => $meal_notes));
    }

    //Add the entire table row under the previously existing ID
    $grouped[$id][] = $value;
}

现在我们有一个名为$grouped的数组,您可以使用foreach循环。这个foreach循环应该完全取代你的while循环

<table>
    <tr>
        <td align='center'>Meal ID</td>
        <td align='center'>User ID</td>
        <td align='center'>Date</td>
        <td align='center'>Time</td>
        <td align='center'>Notes</td>
    </tr>
<?php
foreach($grouped as $key => $meals) {
    //access group of arrays classified as "meals"
    ?>
        <tr>
            <td align='center'><?=$key;?></td>
            <td align='center'><?=$meals['user_id'];?></td>
            <td align='center'><?=date("d/m/Y", strtotime($meals['meal_date']));?></td>
            <td align='center'><?=date("g:i A", strtotime($meals['meal_time']));?></td>
            <td align='center'><?=$meals['meal_notes'];?></td>
        </tr>
    <?php
    foreach($meals as $meal) {
        ?>
            <tr>
                <td align='center'><?=$meal['meal_item_name'];?></td>
                <td align='center'><?=$meal['meal_item_measure'];?></td>
                <td align='center'><?=$meal['meal_item_measurement'];?></td>
                <td align='center'></td>
                <td align='center'></td>
            </tr>
        <?php
    }
}
?>
</table>

注意:只要您运行PHP版本&gt; = 5.4.0,就可以使用echo的短语法(即使未启用short_open_tag),这样可以让您的回声看起来更好与HTML结合使用时。

<element><?php echo $variable; ?></element>

VS

<element><?=$variable;?></element>
  echo也有一个快捷语法,你可以在其中立即关注   用等号打开标签。在PHP 5.4.0之前,这个简短的语法   仅适用于启用了short_open_tag配置设置。

     

PHP: echo - Manual

答案 1 :(得分:0)

对于第一个版本,您可以为每餐进行其他数据库调用。

Execute Query Meal
While (Query Meal Results) {
  Echo html for meal
  Build Query Meal Item
  Execute Query Meal Item   
  While (Query Meal Item Results) {
     Echo html for meal item
  }
}

为了获得更好的性能,可以为商店用餐项目创建一个数组,如:

$mealArray[$mealId] = $mealItem;

只有在$ mealArray中不存在$ mealId键时才构建并执行查询餐食。

答案 2 :(得分:0)

假设我们有两餐桌,饭菜_item

$meal_data = 'select * from meal'

echo '<table>';
while($row = mysqli_fetch_assoc($meal_data)){
    ?>
    <tr>
        <td align='center'><?php echo($row['meal_id']);?></td>
        <td align='center'><?php echo($row['user_id']);?></td>
        <td align='center'><?php echo(date("d/m/Y", strtotime($row['meal_date'])));?></td>
        <td align='center'><?php echo(date("g:i A", strtotime($row['meal_time'])));?></td>
        <td align='center'><?php echo($row['meal_notes']);?></td>
    </tr>
    <?php
    $meal_item_data = 'select * from meal_item where meal_id='.$row['meal_id'];
    while($row_item = mysqli_fetch_assoc($meal_data_item)){
        ?>
        <tr><td align='center'><?php echo($row_item['meal_item_name']);?></td>
            <td align='center'><?php echo($row_item['meal_item_measure']);?></td>
            <td align='center'><?php echo($row_item['meal_item_measurement']);?></td>
        </tr>
        <?php
    }
}
echo '</table>';