Data counting in php

-2

I have a problem and I can not solve it, I need to make a code that searches the data in the bank and counts the total of this data, then I need to separate them from the largest to the smallest, the big problem is that the data repeats , so I need to count that data, and then if it is larger than the others then it appears in the ranking.

For the time being I have this code, but I know that there is a shorter way to do this, I need help, because there are more than 100 different data and each one can have several quantities,

$host = "localhost";
$usuario = "root";
$senha = "";
$banco = "netanalise";
$totime = strtotime("-1 days");
$data = date("Y/m/d",$totime);
$data01=date("Y/m/d");
$dataTabela=date("d/m/Y",$totime);

$cont01=1;$cont02=1;$cont03=1;$cont04=1;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //para o total   
$c01= mysqli_connect($host, $usuario, $senha) or trigger_error(mysqli_error(),E_USER_ERROR); 
// seleciona a base de dados em que vamos trabalhar
mysqli_select_db($c01,$banco);
// cria a instrução SQL que vai selecionar os dados
$query01 = ("SELECT * FROM net_virtua WHERE codBaixa and data BETWEEN CURRENT_DATE()-7 AND CURRENT_DATE()");
// executa a query
$dados_Ant = mysqli_query($con_Ant,$query01) or die(mysqli_error());

// transforma os dados em um array
 while( $linha_cod_virtua = mysqli_fetch_assoc($dados_Ant))
         {
                if($linha_cod_virtua['codBaixa'] == $todos_codigos[0])
                    {
                       $dado = $cont01++;
                    }
                 else if($linha_cod_virtua['codBaixa'] == $todos_codigos[1])
                     {
                        $dado1 = $cont02++;
                     }
                 else if($linha_cod_virtua['codBaixa'] == $todos_codigos[2])
                     {
                        $dado2 = $cont03++;
                     }
                 else if($linha_cod_virtua['codBaixa'] == $todos_codigos[3])
                     {
                       $dado3 = $cont04++;
                     }
         }



    $totalCod = array($dado,$dado1,$dado2,$dado3);
     rsort($totalCod);  
    
asked by anonymous 19.10.2017 / 03:41

1 answer

0

If I understood the situation correctly, this query would bring the sums already ordered, for each codBaixa that exists in the period:

SELECT 
    codBaixa,
    COUNT(*) AS total
FROM net_virtua 
WHERE data BETWEEN CURRENT_DATE()-7 AND CURRENT_DATE()
GROUP BY codBaixa
ORDER BY COUNT(*) DESC
    
19.10.2017 / 03:54