Add range of BD values in PHP

0

I'm doing a project in which the user can select a range of months (eg January-May) and I select the data available in my database to add them using PHP. This I must do for five different venues. The algorithm is like this:

+seleccione meses: enero-marzo
consolidadosede1=sede1_unidades_enero+sede1_unidades_febrero+sede1_unidades_marzo;
consolidadosede2=sede2_unidades_enero+sede2_unidades_febrero+sede2_unidades_marzo;

... so on until the five campuses are completed. I appreciate your help.

    
asked by Ronel Jimenez 23.02.2017 в 16:54
source

1 answer

0

You can use the array_reduce function assuming that:

$datos_sede1 = [
    'sede1_unidades_enero' => 100,
    'sede1_unidades_febrero' => 50,
    'sede1_unidades_marzo' => 40
];

$consolidadosede1 = array_reduce($datos_sede1, function($carry,$item){
    return $carry + $item;
});

// el valor de $consolidadosede1 es 190

But it would be easier if you do it in your sql query

#Usando Mysql
#Esta consulta es del 1 de enero de 2010 al 1 marzo del mismo año
SELECT 
    sum(unidades) as consolodadosede1 
FROM 
    sede1 
WHERE fecha BETWEN '2010-01-01' AND '2010-03-01';

If you can provide more information, surely it could be easier to help you

    
answered by 23.02.2017 / 18:35
source