Jump to content

Printing multiple arrays or array subsets


thenorman138

Recommended Posts

My code is creating an array and I have it printing to ensure that it has the right values, which it does.
 
The problem is it seems to be creating too many arrays or array subsets in the loop. 
 
Each array should be for one invoice and look like this:
 
    [0] => Array
    (
        [iNVOICE] => 12605987
        [DEALER] => 12314
        [FRAME] => 43845
        [COVER] => 1228
        => 49
        [sHIPDATE] => 20170517
        [QUANTITY] => 2
    )
 
This is how it's currently printing for each invoice, and it's pulling all the right invoices and data, just too many:
 
 
    Array
    (
    [0] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 43845
            [COVER] => 1228
            => 49
            [sHIPDATE] => 20170517
            [QUANTITY] => 2
        )
 
    )
    Array
    (
    [0] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 43845
            [COVER] => 1228
            => 49
            [sHIPDATE] => 20170517
            [QUANTITY] => 2
        )
 
    [1] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 40404
            [COVER] => 1223
            => 29
            [sHIPDATE] => 20170602
            [QUANTITY] => 1
        )
 
    )
    Array
    (
    [0] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 43845
            [COVER] => 1228
            => 49
            [sHIPDATE] => 20170517
            [QUANTITY] => 2
        )
 
    [1] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 40404
            [COVER] => 1223
            => 29
            [sHIPDATE] => 20170602
            [QUANTITY] => 1
        )
 
    [2] => Array
        (
            [iNVOICE] => 12605987
            [DEALER] => 12314
            [FRAME] => 546892
            [COVER] => 2220
            => 29
            [sHIPDATE] => 20170602
            [QUANTITY] => 1
        )
 
    )
 
I'm including the parts of the script containing all values and arrays that this section utilizes to help with clearing it up, as well as where I'm seeing the issue, notated in the code by /**/
 
$orderShippedCheck = "
SELECT 
order_id,
order_status
FROM order_status
WHERE order_status = 'S'
";


$result = mysqli_query($mysqlConn, $orderShippedCheck);
$order_ids = array();


//loop results to gather order IDs and store them
while ($row = mysqli_fetch_array($result)){
    $order_ids[] = $row['order_id'];
}


foreach($order_ids as $order_id){


//SELECT FROM DB2 WITH THE ORDER NUMBERS FIRST
$DB2Shipped = " 
SELECT  
invnoc as INVOICE,
        cstnoc AS DEALER,
        framec AS FRAME,
        covr1c AS COVER,
        colr1c AS COLOR ,
        extd2d AS SHIPDATE,
        orqtyc AS QUANTITY
FROM GPORPCFL
WHERE invnoc = '{$order_id}'
group by invnoc,cstnoc, slsnoc, orqtyc, framec, covr1c,colr1c, extd2d
order by invnoc asc
";


print_r($order_id); //Prints correct order ID before each array for that order ID


$Db2ShipRslt = odbc_exec($DB2Conn,$DB2Shipped);
if ( $Db2ShipRslt === false ) {
     exit (odbc_errormsg($DB2Conn));
}


$Db2ShipArr = array();
print_r($order_id);


while($db2ShipRow = odbc_fetch_array($Db2ShipRslt)){
{
    $Db2ShipArr[] = $db2ShipRow;
    }


foreach($Db2ShipArr as $Db2Ship){


}
print_r($Db2ShipArr);  /*This is where the array is printing in that format*/
}

 

Link to comment
Share on other sites

The output problem is due to this:

 

You create an array and APPEND each record to the array in the loop

$Db2ShipArr[] = $db2ShipRow;

Then you output that array

print_r($Db2ShipArr);

So, on the first iteration of the loop, that array contains the first record and outputs just that one record. Ont eh second iteration of the loop it contains the first and second records (and outputs both records). On the third iteration it would output records 1, 2, & 3.

 

Also, you don't need to (and shouldn't) run that other query within a loop. Run ONE query to get all the data.

<?php
 
$query = "SELECT order_id
          FROM order_status
          WHERE order_status = 'S'";
$result = mysqli_query($mysqlConn, $query);
$order_ids = array();
//loop results to gather order IDs and store them
while ($row = mysqli_fetch_assoc($result)){
    $order_ids[] = $row['order_id'];
}
 
//Get the order details for ALL the orders in ONE query
$orderIdsStr = "'" . implode("', '", $order_ids) . "'";
//SELECT FROM DB2 WITH THE ORDER NUMBERS FIRST
$query = "SELECT invnoc as INVOICE,
                 cstnoc AS DEALER,
                 framec AS FRAME,
                 covr1c AS COVER,
                 colr1c AS COLOR ,
                 extd2d AS SHIPDATE,
                 orqtyc AS QUANTITY
          FROM GPORPCFL
          WHERE invnoc IN ({$orderIdsStr})
          GROUP BY invnoc,cstnoc, slsnoc, orqtyc, framec, covr1c,colr1c, extd2d
          ORDER BY invnoc asc";
$Db2ShipRslt = odbc_exec($DB2Conn, $query);
if ( $Db2ShipRslt === false ) {
     exit (odbc_errormsg($DB2Conn));
}
 
//Process the results
$Db2ShipArr = array();
while($db2ShipRow = odbc_fetch_array($Db2ShipRslt)){
{
    //Output the record
    print_r($db2ShipRow);
    //Append record to results array
    $Db2ShipArr[] = $db2ShipRow;
}
 
?>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.