I have a table where I store transfers. each transfer has a date , and a number of movil
. I need to make a monthly statistics, the number of transfers per day per movil
.
The query works great, but the problem is that I need to show each day in a column, and each movil
in a row.
My idea was to keep all result in a multidimensional array. But it is very slow then to search for values .
This is the transfers table:
id fecha movil
1 01/08/2016 A
2 01/08/2016 A
3 01/08/2016 B
4 02/08/2016 A
5 02/08/2016 B
6 02/08/2016 B
7 02/08/2016 C
8 02/08/2016 A
9 03/08/2016 A
10 03/08/2016 B
This is my query:
select DAY(fecha),movil, count(id)
from traslados
where YEAR(fecha) = '2016' and MONTH(fecha) = '07' and movil is not null
group by DAY(fecha),movil
Now, I have to show the results in a table with the following format:
movil/fecha 1 2 3
A 2 1 1
B 1 2 1
C 0 1 0
Is there a way I can do this in the query, or in php code?
ADDED
this is how the result of my query looks:
DAY(FECHA) MOVIL COUNT
1 A 2
1 B 1
2 A 2
2 B 2
2 C 1
3 A 1
3 A 1
It has all the data I want, but not the format.
This was my attempt:
<?
$maxDias = cal_days_in_month(CAL_GREGORIAN, '07', '2016');
$cons = "select DAY(fecha) as dia,movil, count(idTraslado) as cantidad
from traslados
where YEAR(fecha) = '2016' and MONTH(fecha) = '07' and movil is not null
and (idObraSocial = 20 or idObraSocial = 5)
group by DAY(fecha),movil";
$result = mysql_query($cons, $conexion);
$filas = mysql_affected_rows();
$array = array();
while($data = mysql_fetch_assoc($result)){
$array[] = $data;
}
print_r($array);
$consMoviles = "select DISTINCT movil from traslados
where YEAR(fecha) = '2016' and MONTH(fecha) = '07' and movil is not null
and (idObraSocial = 20 or idObraSocial = 5)
order by movil";
$resultMoviles = mysql_query($consMoviles, $conexion);
$cantMoviles = mysql_affected_rows();
?>
<table class="table table-hover table-bordered">
<thead>
<tr class="success">
<th>
Movil/Día
</th>
<?for ($i = 1; $i < $maxDias; $i++) {?>
<th>
<?echo $i;?>
</th>
<?}?>
</tr>
</thead>
<tbody>
<? while ($rowMovil = mysql_fetch_array($resultMoviles, MYSQL_ASSOC)) { ?>
<tr>
<th>
<?=$rowMovil["movil"];?>
</th>
<?for ($i = 1; $i < $maxDias; $i++) {?>
<th>
HERE SHOW DATA
</th>
<?}?>
</tr>
<?}?>
</tbody>
</table>