简体   繁体   English

为什么我的条件语句没有在 foreach 循环中多次执行? PHP

[英]Why is my conditional statement not executing multiple times in a foreach loop? PHP

Im trying to print out order information that matches to a list of customers depending on if the customer id on the order matches the customer id of the customer.我试图打印出与客户列表匹配的订单信息,具体取决于订单上的客户 ID 是否与客户的客户 ID 匹配。 Both customers and orders are stored in arrays and so far I don't have issues with executing my order table and printing my information.客户和订单都存储在数组中,到目前为止,我在执行订单表和打印信息方面没有问题。 The problem I am having is that only one order is printing for the customer and some customers have multiple orders which should be printing.我遇到的问题是只有一个订单正在为客户打印,而有些客户有多个订单应该打印。 Ive used a foreach loop to iterate through the orders matching order id to requested customer id, which is coming from a query string.我使用 foreach 循环遍历匹配订单 ID 的订单到请求的客户 ID,它来自查询字符串。 I cant tell why my if statement is not printing for multiple orders when I know that at least 1 is printing.当我知道至少有 1 个正在打印时,我不知道为什么我的 if 语句没有为多个订单打印。

Here is the code for my customer table that executes when the query string is returned with an id of a customer.这是我的客户表的代码,当查询字符串返回客户 ID 时执行。 The foreach loop at the bottom is where I'm expecting all matching orders to print but it is only giving me one order when I expect in some cases multiple.底部的 foreach 循环是我希望打印所有匹配订单的地方,但当我希望在某些情况下有多个订单时,它只给我一个订单。 If there's any other information needed I can supply it.如果需要其他信息,我可以提供。 Thanks谢谢

<?php

if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if (isset($_GET['customer'])) {

        $requestedCustomer = $customers[$_GET['customer']];

        $orders = readOrders('orders.txt');

        // <!-- orders panel -->
        echo '<br><br><br>';
        echo '<div class="panel panel-danger spaceabove">';
        echo '<div class="panel-heading">';
        echo '<h4>Orders for ' . $requestedCustomer['name'] . '</h4>';
        echo '</div>';
        echo '<table class="table">';
        echo '<tr>';
        echo '<th>ISBN</th>';
        echo '<th>Title</th>';
        echo '<th>Category</th>';
        echo '</tr>';

        foreach ($orders as $order) {

            if ($requestedCustomer['id'] == $order['id']) {
                echo '<tr>';
                echo '<td>' . $order['isbn'] . '</td>';
                echo '<td>' . $order['title'] . '</td>';
                echo '<td>' . $order['category'] . '</td>';
                echo '</tr>';
            }
        }
    }
    echo '</table>';
    // foreach ($orders as $order) {
    // if ($order['id'] == $requestedCustomer['id']) {

    // }
}

?>

Have you checked why this happens?您是否检查过为什么会发生这种情况? A single ID cannot be used multiple time in the same array, so if you parse multiple rows with the same ID and write them into the array using the same ID , you are overwriting prior orders.一个单一的ID不能使用多个时间在同一阵列中,因此,如果解析多个行具有相同的ID,并将其写入到使用相同的ID,要覆盖现有的订单阵列。 Only the last order with that ID is accessible.只能访问具有该 ID 的最后一个订单。 By comparing the parsed array of orders and the file containing your orders, that should be visible pretty easy.通过比较解析的订单数组和包含订单的文件,这应该很容易看到。

That's why you usually use unique order IDs, and store the user ID within that order这就是为什么您通常使用唯一的订单 ID,并将用户 ID 存储在该订单中

I think your problem is in readOrders .我认为您的问题出在readOrders You appear to be clobbering your array each time with $orders[$order['id']] = $order;你似乎每次都用$orders[$order['id']] = $order;来破坏你的数组$orders[$order['id']] = $order; Instead you want $orders[$order['id']][] = $order;相反,您想要$orders[$order['id']][] = $order;

function readOrders($filename)
{
    $arr = file($filename) or die('ERROR: Cannot find file');

    // Create our primary order array
    $orders = [];

    $delimiter = ',';

    // reading in customers from file and storing in array of customers

    foreach ($arr as $line) {

        $splitcontents = explode($delimiter, $line);

        $order = array();

        $order['id'] = $splitcontents[1];
        $order['isbn'] = $splitcontents[2];
        $order['title'] = utf8_encode($splitcontents[3]);
        $order['category'] = utf8_encode($splitcontents[4]);

        // Append the order to the array, 
        $orders[$order['id']][] = $order;
    }
    return $orders;
}

Then, in your print loop, you don't loop over $orders , you instead take the main orders array which is indexed by the customer ID and grab those specific orders.然后,在您的打印循环中,您不会遍历$orders ,而是使用由客户 ID 索引的主订单数组并获取这些特定订单。

// Get the orders for this specific customer, if they exist
$customerOrders = $orders[$requestedCustomer['id']] ?? [];
foreach ($customerOrders as $order) {
    echo '<tr>';
    echo '<td>' . $order['isbn'] . '</td>';
    echo '<td>' . $order['title'] . '</td>';
    echo '<td>' . $order['category'] . '</td>';
    echo '</tr>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM