Data count in php

Asked

Viewed 38 times

-2

I have a problem and I can not solve, I need to make a code that searches the data in the database and makes the total count of these data, then I need to separate them from the largest to the smallest, the big problem is that the data is repeated, then I need to count that data and then if it is greater than the quantity of the others then it appears in the ranking.

For now I have this code below 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, I need to know who is the may

$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);  
  • 2

    Your question is very confusing. There are ways to add to it the structure of your table in the database and examples of what these records would look like?

  • 1

    Can’t you provide the database structure + a mass of data? I needed to understand your database (what this data is) in order to understand the PHP code. I didn’t understand where the value of $todos_codigos

  • 1

    WHERE codBaixa and doesn’t make sense. And you can probably count and sort only in SQL.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.