【问题标题】:php foreach loop only returns values for first iteration in arrayphp foreach 循环仅返回数组中第一次迭代的值
【发布时间】:2023-03-03 04:24:01
【问题描述】:

我似乎无法确定为什么我的 foreach 循环能够循环创建所有 5 个 ProductionOrderID,但只返回第一个 ID 的数据。

据我了解,数组循环正确,您可以在此处看到 当前结果https://i.imgur.com/JWD3nis.png 但奇怪的是 ID:2 没有生成表,而 ID 5 有 2 个表已创建,根据刚刚链接的 imgur 屏幕截图全部为空白。

我已经仔细检查了我的示例数据,每个表都有 5 条唯一记录,我找不到任何重复或问题。

编辑:1 我忘了提到想要的结果来阐明我希望循环如何工作。请看这个截图:https://i.imgur.com/4h7l49p.png (Cheers Sand)。

编辑:2 这是 SQL 的导出:https://pastebin.com/MG2gtASu 这是我的ERD,如果它有帮助的话:https://i.imgur.com/idVR5ev.png

编辑:3 新的、更新的代码(感谢 Sand):

<?php
include('OrderCore/connect-db.php');
$POIds = array();
if ($result = $mysqli->query("SELECT ProductionOrderID FROM ProductionOrder" ) ) {
    while ($row = $result->fetch_object()) {
        $POIds[] = $row->ProductionOrderID;
    }
}
foreach ( $POIds as $index => $OrderId ) {
    if ( $result = $mysqli->query("
    SELECT * 
    FROM ProductionOrder AS p
    LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID ) 
    LEFT JOIN NotGood AS n ON ( p.ProductionOrderID = n.NGID ) 
    LEFT JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.BatchID ) 
    LEFT JOIN Brand AS bd ON ( p.ProductionOrderID = bd.BrandID ) 
    LEFT JOIN CustomerOrder AS co ON ( p.ProductionOrderID = co.COID ) 
    LEFT JOIN Customer AS c ON ( p.ProductionOrderID = c.CustomerID ) 
    LEFT JOIN CustomerOrderStatus AS cos ON ( p.ProductionOrderID = cos.COStatusID ) 
    WHERE p.ProductionOrderID='$OrderId'") ) {
        while( $row = $result->fetch_object() ) {
            print "<h1>Order: $OrderId</h1>";
            print "<table class='table table-striped'>";
            print "<tr> <th>PO ID</th> <th>PO #</th> <th>Order Quantity</th> <th>Balance Left</th> <th>Production Date</th> <th>Production Order Status</th> <th>Not Good ID</th> </tr>";
            print "<td>" . $row->ProductionOrderID . "</td>";
            print "<td>" . $row->PONum . "</td>";
            print "<td>" . $row->OrderQTY . "</td>";
            print "<td>" . $row->BalLeftNum . "</td>";
            print "<td>" . $row->ProductionDate . "</td>";
            print "<td>" . $row->ProductionOrderStatusID . "</td>";
            print "<td>" . $row->NGID . "</td>";
            print "</tr>";
            print "</table>";
            //BatchOrder
            print "<table class='table table-striped'>";
            print "<tr> <th>Batch ID</th> <th>Brand Name</th> <th>Batch Quantity</th> <th>Availability Date</th> <th>Remaining Balance</th> <th>Production Order ID</th> </tr>";
            print "<td>" . $row->BatchID . "</td>";
            print "<td>" . $row->BrandID . "</td>";
            print "<td>" . $row->BatchQTY . "</td>";
            print "<td>" . $row->AvailDate . "</td>";
            print "<td>" . $row->RemainBal . "</td>";
            print "<td>" . $row->ProductionOrderID . "</td>";
            print "</tr>";
            print "</table>";
            //CustomerOrder
            print "<table class='table table-striped'>";
            print "<tr> <th>Customer ID</th> <th>Customer Name</th> <th>Invoice Quantity</th> <th>Invoice #</th> <th>Shipping Date</th> <th>Batch ID</th> <th>CO Status</th> </tr>";
            print "<td>" . $row->COID . "</td>";
            print "<td>" . $row->CustomerID . "</td>";
            print "<td>" . $row->InvoiceQTY . "</td>";
            print "<td>" . $row->InvoiceNum . "</td>";
            print "<td>" . $row->ShipDate . "</td>";
            print "<td>" . $row->BatchID . "</td>";
            print "<td>" . $row->COStatusID . "</td>";
            print "</tr>";
            print "</table>";
        }
    }
    else
    {
        print "No results to display!";
    }
}
$mysqli->close();
?>

【问题讨论】:

  • 您想在SQL 每次运行时每页循环 5 条记录或 5 条记录?还是只想在表格中显示记录?
  • 我想循环每条记录(ProductionOrder);目前是 5,但可能是 5 或 500。
  • 好的,你能告诉我$POIds 是做什么的吗?
  • 这是我尝试创建一个数组来存储所有ProductionOrderIDforeach 循环中被称为$OrderId 的数组。结果当前如下所示:i.imgur.com/MIhfB0s.png
  • 是的,我试图弄清楚为什么你要使用foreach 如果你想显示数据表中的内容,只需使用while 循环它就会循环直到没有更多要显示的数据。

标签: php mysql arrays foreach inner-join


【解决方案1】:

我知道为什么会这样了,因为 INNER this 的连接类型会帮助你理解。

找到了为什么它只显示 1 批数据的问题。这是因为您等于p.ProductionOrderID = b.BatchID,所以发生的情况是查询看起来将您的生产 ID 与批次 ID 相匹配,并且您的批次 ID 是唯一的,因此没有重复导致显示来自该匹配行的单个数据记录。您真正想要做的是匹配批处理表中的生产 ID,因为这是您的生产表和批处理表之间的关系。现在,当您运行它时,它将绘制表格直到批次结束。

如果您想在 HTML 的一列中显示所有批次详细信息,请建议 whileforeach 并且您不需要另一个 SQL 您已经选择了行。例如:$row["BatchQTY"]

这是我的解决方案。

if ( $result = $mysqli->query("
    SELECT * 
FROM ProductionOrder AS p
LEFT JOIN ProductionOrderStatus AS s ON ( p.ProductionOrderID = s.ProductionOrderStatusID ) 
LEFT JOIN NotGood AS n ON ( p.ProductionOrderID = n.NGID ) 
LEFT JOIN BatchOrder AS b ON ( p.ProductionOrderID = b.ProductionOrderID)//Changed this equation 
LEFT JOIN Brand AS bd ON ( p.ProductionOrderID = bd.BrandID ) 
LEFT JOIN CustomerOrder AS co ON ( p.ProductionOrderID = co.COID ) 
LEFT JOIN Customer AS c ON ( p.ProductionOrderID = c.CustomerID ) 
LEFT JOIN CustomerOrderStatus AS cos ON ( p.ProductionOrderID = cos.COStatusID )
    WHERE p.ProductionOrderID='$OrderId'")

【讨论】:

  • 非常感谢!!!对于视觉示例,这肯定有助于阐明不同的类型。再次感谢您花时间理解和帮助。
  • 我确实注意到每个生产订单显示的批次或客户订单不超过 1 个。你对如何遍历它们有什么建议>
  • 抱歉,当您发表评论时,我处于离线状态。没问题,你的问题也让我有机会重温我的SQL。关于您的第二条评论是在更正SQL 之后吗?你的意思是当同一 Customer Order 下有更多数据时,你的系统只显示一组数据?您也是使用相同的SQL 还是使用不同的SQL
  • 嗨,沙,别担心。所以我用更新的代码再次更新了我原来的问题,这就是我现在正在使用的。在这里,您可以看到它对每个新的 ProductionOrder 都非常有效:i.imgur.com/JXNzEfS.png 唯一的问题是它只向我显示 1 个 BatchOrder 和每个 ProductionOrder 的 1 个客户订单。可以有超过 1 个 BatchOrder 附加到 ProductionOrder 并且可以有超过 1 个 CustomerOrder 附加到 BatchOrder。我花了一段时间试图在循环中创建一个循环,但到目前为止没有太大成功!
  • 啊,好吧,我认为这可以通过使用 SQL 它自己来完成,你能帮我一个忙,给我一个带有一些数据的 SQL 粘贴箱,我可以重新创建你所说的场景,这样我就可以我自己测试一下。
【解决方案2】:

在while循环中输入

echo "<pre>";
print_r($row);
echo "</pre>";

因此您可以查看数据的行为。我认为主要问题来自您的选择查询

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 2018-07-07
    • 2011-01-17
    • 2020-02-08
    相关资源
    最近更新 更多