PHP While和ForEach没有产生预期的结果

时间:2017-10-06 22:11:05

标签: php mysqli foreach while-loop

我目前正在使用while语句来获取meal_id的每个相关记录,但我的foreach无法正常工作。我的目标是为每一餐找到一餐排,然后在下面显示与该餐相关的餐食,然后循环到下一餐并重复。

目前,结果是显示正确的膳食,但随后显示每个膳食项目下面的每一餐。如:

Meal
Meal Item
Meal Item
Unrelated Meal Item
Unrelated Meal Item
Unrelated Meal Item
Unrelated Meal Item
Unrelated Meal Item
Unrelated Meal Item
Unrelated Meal Item

什么时候该做:

Meal 1
Meal Item
Meal Item

Meal 2
Meal Item

Meal 3
Meal Item
Meal Item
Meal Item
Meal Item

我也想获得该餐中的食品数量,并在餐桌的餐行中显示。 这是我的代码到目前为止,你可以提供任何帮助将是惊人的! : - )

<?php
$con=mysqli_connect($DBServer,$DBUser,$DBPass,$DBName);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
    $view = 'empty';
if(isset($_GET["v"]))
    $view = $_GET["v"];

switch($view)
{
    default:
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id WHERE DATE(meal_date) = CURDATE() ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "Today (default view)";
        break;
    case 'all':
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "All Records";
        break;
    case 'today':
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id WHERE DATE(meal_date) = CURDATE() ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "Today";
        break;
    case 'yesterday':
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id WHERE DATE(meal_date) = CURDATE() - INTERVAL 1 DAY ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "Yesterday";
        break;
    case 'week':
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id WHERE meal_date BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "Last 7 Days";
        break;
    case "fortnight":
        $result = mysqli_query($con,"SELECT * FROM tblmeals INNER JOIN tblmealitems ON tblmeals.meal_id = tblmealitems.meal_id WHERE meal_date BETWEEN CURDATE() - INTERVAL 14 DAY AND CURDATE() ORDER BY meal_date DESC, meal_time DESC");
        $viewstatus = "Last 14 Days";
        break;
} ?><br>

<div class="viewdesc">Current View: <strong><?php echo($viewstatus); ?></strong></div><br>
<br>
<?php 

    // Start record fail error checking

    if (!$result) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}
    // Start record fail error checking */

    if (mysqli_num_rows($result) === 0) {?>
          <div id="resultserror">
No results could found for the selected view.</div>
          <?php } else { ?>
              <table id="results" width="100%" cellpadding="3" cellspacing="0">
<tr>
<th align="center">Meal #</th>
<th align="center">User #</th>
<th align="center">Meal Items</th>
<th align="center">Meal Date</th>
<th align="center">Meal Time</th>
<th align="center">Meal Notes</th>
</tr>
<?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("Items: " . mysqli_num_rows($result) . "");?></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 foreach ($result as $row)
{?>
<tr>
<td align='center' colspan='4'><?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
}
}
?>
<tfoot>
<tr>
  <td colspan="8" align="center">- end of report -</td></tr></tfoot>
</table>
<?php
mysqli_close($con);
                     }?>

1 个答案:

答案 0 :(得分:2)

使用2个查询而不是out = (idx[...,None] == np.arange(dist.shape[1])).any(1).astype(int) 来重新编写代码。

请注意,我不喜欢像这样交织HTML和PHP标签 - 但这可能是个人偏好而不是这个问题的话题:

idx = np.argpartition(dist, 2, axis=1)[:,:2]