Home > Software design >  Why is my conditional statement not executing multiple times in a foreach loop? PHP
Why is my conditional statement not executing multiple times in a foreach loop? PHP

Time:11-13

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 >';
        echo '<div >';
        echo '<h4>Orders for ' . $requestedCustomer['name'] . '</h4>';
        echo '</div>';
        echo '<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']) {

    // }
}

?>

CodePudding user response:

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

CodePudding user response:

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>';
}
  • Related