0

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>
4
  • What's the result of the query? Does it have the data you expect? What attempt have you made to display it?
    – David
    Aug 9, 2016 at 18:10
  • mysql_fetch_array might be your friend here, although I guess you want the full code...
    – Neobugu
    Aug 9, 2016 at 18:13
  • i added data to the question. Aug 9, 2016 at 18:21
  • can i search a value in my array, having the day and the movil, without iteration ? beacuse i have about 30 moviles and 31 days of the month, it will turn ver slow !!! Aug 9, 2016 at 18:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.