Search code examples
phparraysloopsmultidimensional-array

Loop only stores the last row while trying to build a 2d array while looping a query result set


I am having trouble in forming multidimensional array. I am receiving data from database, then I need to create array which will be sent over api to make packing.

First I get values from form

$idNaloga = $_GET['idNaloga'];
$duzina = $_GET['duzina'];
$sirina = $_GET['sirina'];
$visina = $_GET['visina'];

First I make query for trailer:

$truckTrailer = array(
    'w' => $sirina,
    'h' => $visina,
    'd' => $duzina,
    'max_wg' => '15000',
    'id' => $kamion1
);

Then I use those values to make query

$stmt = "SELECT DISTINCT(nazivPanela) as paket FROM `paneli_pakovanja_2017` WHERE idNaloga = $idNaloga";

$q = $conn->query($stmt);

$nazivPaketa = array();

while($r = $q -> fetch()){  
    $nazivPaketa[] = $r['paket'];
}

Once I get names of packets, I make query for each packet. In this query I should get for each packet array that looks

array(
    'w' => '100',
    'h' => '90',
    'd' => '1350',
    'q' => '18',
    'vr' => '1',
    'wg' => '0',
    'id' => 'Paket'
),

But I dont, I get only one array from the query below, but I should get 7.

foreach($nazivPaketa as $paket) {

$stmt2 = "SELECT max(duzina) as duzina, visinaPaketa, sirinaPaketa, nazivPanela FROM `paneli_pakovanja_2017` WHERE idNaloga = $idNaloga AND nazivPanela = $paket";

$q = $conn -> query($stmt2);

while($r=$q->fetch()) {

    $paketi = array(
        'w' => $r['sirinaPaketa'],
        'h' => $r['visinaPaketa'],
        'd' => $r['duzina'],
        'q' => '1',
        'vr' => '1',
        'wg' => '0',
        'id' => $r['nazivPanela'] 
    );
}
}

At the end I need form array which will look like:

$data = array( 

'bins' => array( // this array I have it is truck trailer

                array(
                        'w' => '250',
                        'h' => '360',
                        'd' => '1360',
                        'max_wg' => '15000',
                        'id' => '1'
                    )
            ),

'items' => array(  *// array I should get from $paketi
                array(
                        'w' => '100',
                        'h' => '90',
                        'd' => '1350',
                        'q' => '18',
                        'vr' => '1',
                        'wg' => '0',
                        'id' => 'Paket'
                    ),
                array(
                        'w' => '100',
                        'h' => '90',
                        'd' => '900',
                        'q' => '23',
                        'vr' => '1',
                        'wg' => '0',
                        'id' => 'Paket2'
                ),
                array(
                    'w' => '100',
                    'h' => '90',
                    'd' => '400',
                    'q' => '18',
                    'vr' => '1',
                    'wg' => '0',
                    'id' => 'Paket'
                )
            ),

Don't know am I using right way, but need some kind of assistance


Solution

  • Not tested but perhaps you could try like below. The variable $paketi is being overwritten on each iteration in loop but you need to store it's records for all queries so declare an array before the outer loop and add contents from inner loop to it

    $data=array();
    
    foreach( $nazivPaketa as $paket ) {
    
        $stmt2 = "SELECT max(duzina) as duzina, visinaPaketa, sirinaPaketa, nazivPanela 
            FROM `paneli_pakovanja_2017` 
            WHERE idNaloga = $idNaloga AND nazivPanela = $paket";
    
        $q = $conn->query( $stmt2 );
    
        $paketi=array();
    
        while( $r=$q->fetch() ) {
            $paketi[] = array(
                'w' => $r['sirinaPaketa'],
                'h' => $r['visinaPaketa'],
                'd' => $r['duzina'],
                'q' => '1',
                'vr' => '1',
                'wg' => '0',
                'id' => $r['nazivPanela'] 
            );
        }
        $data[]=$paketi;
    }
    
    echo '<pre>',print_r($data,1),'</div>';
    

    To get the other array added you could try:

    $data['bins']=$truckTrailer; /* assuming `$truckTrailer` is an array */