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

  • 本文关键字:循环 ID PHP 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   |

etc:-)

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

首先,需要更多的设置。您应该稍微更改一下您的查询。

您的查询需要按meal_id列进行排序。例如CCD_ 5。

完成后,在当前的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版本>=5.4.0,您可以使用echo的短语法(即使没有启用short_open_tag),这允许您的echo在与HTML结合时看起来更好。

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

<element><?=$variable;?></element>

echo还有一个快捷语法,您可以立即按照用等号打开标记。在PHP 5.4.0之前,这个简短的语法仅在启用short_open_tag配置设置的情况下工作。

PHP:echo-手动

假设我们有两桌饭,meal_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>';

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

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键的情况下,才能构建和执行查询餐项。

最新更新