I have a problem with two arrangements that I need to join, my intention is the following, by means of a query to MySQL with PHP I recover the name of all my registered companies and the income for each month of said companies:
This is my query:
$totalEmpresas = $db->rawQuery('SELECT fecha, SUM(total) AS ingresosTotal from ingresos
where YEAR(fecha) = "2021" AND ingresos.id_rol = ? AND ingresos.id_usuario = ? AND ingresos.empresa = ?
GROUP BY MONTH(fecha)', Array ($idRolUser, $idUser, $idEmpresa));
This query returns me an array grouped by all the available months of each of the consulted companies, this is an example of the array that it returns:
Array
(
[0] => Array
(
[fecha] => 2021-01-08
[ingresosTotal] => 158181.17
)
[1] => Array
(
[fecha] => 2021-02-05
[ingresosTotal] => 781595.14
)
[2] => Array
(
[fecha] => 2021-03-05
[ingresosTotal] => 345094.65
)
[3] => Array
(
[fecha] => 2021-04-09
[ingresosTotal] => 166694.76
)
[4] => Array
(
[fecha] => 2021-05-26
[ingresosTotal] => 155874.65
)
[5] => Array
(
[fecha] => 2021-06-04
[ingresosTotal] => 159801.81
)
[6] => Array
(
[fecha] => 2021-07-02
[ingresosTotal] => 161689.9
)
)
This is where I have the problem my intention is to form an array that has the following structure:
data:[
{
empresa = Empresa Ejemplo 1,
2021-02-05 = 781595.14,
2021-03-05 = 345094.65,
2021-04-09 = 166694.76,
2021-05-26 = 155874.65,
2021-06-04 = 159801.81,
2021-07-02 = 161689.9
},
{
empresa = Empresa Ejemplo 2,
2021-02-05 = 781595.14,
2021-03-05 = 345094.65,
2021-04-09 = 166694.76,
2021-05-26 = 155874.65,
2021-06-04 = 159801.81,
2021-07-02 = 161689.9
}
]
The problem is that my company and my dates are in two separate arrays, and if the one with array_push is as follows:
{"data":[
{
"Empresa":"Empresa Ejemplo 1",
"Fechas":[
{"January":158181.17},
{"February":781595.14},
{"March":345094.65},
{"April":166694.76},
{"May":155874.65},
{"June":159801.81},
{"July":161689.9}
]
}
I tried using array_push inside foreach but this doesn't work work, this is my code:
function MensualIngreso($idRolUser, $idUser){
require_once ("../vendor/thingengineer/mysqli-database-class/MysqliDb.php");
$db = new MysqliDb (DB_HOST, DB_USERNAME, '', DB_NAME);
// Debemos cosultar los ingresos de las empresas globalmente y por mes
// 1.- Seleccionaremos todas las empresas por rol y id de usuario
$db->where ("id_rol", $idRolUser);
$db->where ("id_usuario", $idUser);
$empresas = $db->get ("empresas");
// 2.- Por cada de una de las empresas recorreremos los meses que ha pagado
$aura = [];
$data['data'] = [];
foreach ($empresas as $key => $value) {
$idEmpresa = $value['id_empresa'];
$empresa = $value['empresa'];
$fechas = [];
$totalEmpresas = $db->rawQuery('SELECT fecha, SUM(total) AS ingresosTotal from ingresos
where YEAR(fecha) = "2021" AND ingresos.id_rol = ? AND ingresos.id_usuario = ? AND ingresos.empresa = ? GROUP BY MONTH(fecha)', Array ($idRolUser, $idUser, $idEmpresa));
//print_r($totalEmpresas);
//return;
foreach ($totalEmpresas as $key => $value) {
array_push($fechas, array(strftime("%B", strtotime( $value['fecha'] )) => $value['ingresosTotal']) );
}
//print_r($fechas);
$consulta = array(
"Empresa" => $empresa,
"Fechas" => $fechas
);
array_push($data['data'], $consulta);
}
//print_r($data);
return $data;
}
CodePudding user response:
I think one of your main issues might be that you're trying to get the month from $value['fecha_aportacion']
but that does not appear to exist in your SQL query. I think it should probably just be $value['fecha']
.
I think this does what you're looking for:
$array = [
[
'fecha' => '2021-01-08',
'ingresosTotal' => '158181.17',
],
[
'fecha' => '2021-02-05',
'ingresosTotal' => '781595.14',
],
[
'fecha' => '2021-03-05',
'ingresosTotal' => '345094.65',
],
[
'fecha' => '2021-04-09',
'ingresosTotal' => '166694.76',
],
[
'fecha' => '2021-05-26',
'ingresosTotal' => '155874.65',
],
[
'fecha' => '2021-06-04',
'ingresosTotal' => '159801.81',
],
[
'fecha' => '2021-07-02',
'ingresosTotal' => '161689.9',
],
];
$empresa = 'empresa ejemplo 1';
$fechas = [];
$fechas['empresa'] = $empresa;
foreach ($array as $value) {
$fecha = strftime("%B", strtotime( $value['fecha'] ));
$fechas[$fecha] = $value['ingresosTotal'];
}
echo json_encode($fechas, JSON_PRETTY_PRINT);
Output:
{
"empresa": "empresa ejemplo 1",
"January": "158181.17",
"February": "781595.14",
"March": "345094.65",
"April": "166694.76",
"May": "155874.65",
"June": "159801.81",
"July": "161689.9"
}