简体   繁体   中英

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. 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. I cant tell why my if statement is not printing for multiple orders when I know that at least 1 is printing.

Here is the code for my customer table that executes when the query string is returned with an id of a customer. 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. 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. Only the last order with that ID is accessible. 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

I think your problem is in readOrders . You appear to be clobbering your array each time with $orders[$order['id']] = $order; Instead you want $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.

// 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>';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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