array in a php foreach

0

I will try to be objective in the doubt:

I have the following code:

<?php

foreach ($modelo as $k => $v) {
    $sql_ = "SELECT * FROM pedido WHERE modelo LIKE '%".$v."%' ORDER BY id ASC";
    $disp_sql_ = $mysqli->query($sql_);

    while ($data_ = $disp_sql_->fetch_array()) { 
        $quantidade_    += ($data_['total'] * $data_['kit']);
    }
}

The total of the variable $quantidade , has the output with sum, of all the models.

In the database it looks like this:

valor 1 = 20 ($modelo 1)
valor 2 = 30 ($modelo 1)
valor 3 = 50 ($modelo 1)
valor 4 = 50 ($modelo 2)
valor 5 = 50 ($modelo 2)

The variable $quantidade brings me this:

$quantidade = 200;

What I would like is for you to bring me the result as follows:

$quantidade = array([0] => 100, [2] => 100)

A summation of each template change (every foreach change), results in an array.

    
asked by anonymous 25.09.2018 / 19:23

3 answers

4

You can simply separate the values in the array within your for as follows:

<?php

$quantidade = [];

foreach ($modelo as $k => $v) {
    $sql_ = "SELECT * FROM pedido WHERE modelo LIKE '%".$v."%' ORDER BY id ASC";
    $disp_sql_ = $mysqli->query($sql_);

    while ($data_ = $disp_sql_->fetch_array()) { 
        // Se a chave não existir cria uma nova com o valor 0
        $quantidade[$data_['id_modelo']] = $quantidade[$data_['id_modelo']] ?? 0;

        // Soma normalmente
        $quantidade[$data_['id_modelo']] += $data_['total'] * $data_['kit'];
    }
}

However, a better way would be to do these calculations in the database, using the SUM() together with GROUP BY . Example:

<?php

$quantidade = [];

foreach ($modelo as $k => $v) {
    $sql_ = <<<SQL
        SELECT 
            id_modelo,
            SUM(total * kit) as soma
        FROM pedido 
        WHERE modelo LIKE '%$v%'
        GROUP BY id_modelo,
        ORDER BY id ASC;
SQL;
    $disp_sql_ = $mysqli->query($sql_);

    while ($data_ = $disp_sql_->fetch_array()) { 
        // Já retorna o somatório do banco de dados
        $quantidade[$data_['id_modelo']] = $data_['soma']];
    }
}
    
25.09.2018 / 19:40
0

A simple way to solve this:

<?php

foreach ( $modelo as $k => $v ) {
    $sql_      = "SELECT * FROM pedido WHERE modelo LIKE '%".$v."%' ORDER BY id ASC";
    $disp_sql_ = $mysqli->query( $sql_ );

    $ultimo_valor = 0;
    while ( $data_ = $disp_sql_->fetch_array() ) { 
        $quantidade_[$k] = $ultimo_valor + ( $data_['total'] * $data_['kit'] );
        $ultimo_valor   += ( $data_['total'] * $data_['kit'] );
    }
}

Print how it will look:

Array
(
    [0] => 20
    [1] => 50
    [2] => 100
    [3] => 150
    [4] => 200
)
    
25.09.2018 / 19:56
0

You can initialize the amount outside the loop

<?php
$quantidade_ = array();
foreach ($modelo as $k => $v) {
    $sql_ = "SELECT * FROM pedido WHERE modelo LIKE '%".$v."%' GROUP BY id ASC";
    $disp_sql_ = $mysqli->query($sql_);

    while ($data_ = $disp_sql_->fetch_array()) { 
        $quantidade_[] =  array(
               ($data_['total'] * $data_['kit']);
           )
    }
}

Or boot like this:

 $quantidade_ = []

and feed like this:

 $quantidade[] =  ($data_['total'] * $data_['kit']);
    
25.09.2018 / 19:46