count equal text from php database

Asked

Viewed 209 times

2

Good morning, my doubt is as follows: in my case I have a table in the database that allows to evaluate a service, the classification is made by colors "red, yellow and green", ie the type is text.
What I wanted was to make a query in php that allows counting how many "green", "yellow" and "red" are inserted.
This is the table:

inserir a descrição da imagem aqui


What I want is for example no day 06-06 there were 3 greens, 2 yellows and 1 red, and no day 07-06 there was 1 green. In question to date, I have a calendar I made with jquery and Ajax.
I tried something like this

$verificar=mysqli_query($link,"SELECT classificacao, count(classificacao) from questionario GROUP by classificacao");

    while ($linha = mysqli_fetch_array($verificar)) {
        if($linha['classificacao'] == 'vermelho')
        {
            $output.='<td style="background-color:#ff6666; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao'].'</td>';
        } 
        elseif($linha['classificacao'] == 'amarelo')
        {
            $output.='<td style="background-color:#ffff80; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao'].'</td>';
        } 
        elseif($linha['classificacao'] == 'verde')
        {
            $output.='<td style="background-color:#80ff80; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao'].'</td>';
        }
        $output.='  
        </tr>  
        ';  
    }


However it does not return any value, can anyone tell me what is wrong? Thank you from now who can help!

  • This classification column, you save the color name or color in Hexadecimal?

  • Name of the colour Rafa

4 answers

2


Ana, first I advise you to use PDO. I modified your sql and php a little bit.

<?php
$pdo = new PDO("mysql:host=HOST;dbname=BASE", "USUARIO", "SENHA");

try {
  $stmt = $pdo->prepare("SELECT count(cod) as total, classificacao from questionario group by classificacao");
  $stmt->execute();
  $output = '';
  while ($linha = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $output.='<tr> ';
    if($linha['classificacao'] == 'vermelho')
    {
      $output.='<td style="background-color:#ff6666; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao']." = ".$linha['total'].'</td>';
    }
    elseif($linha['classificacao'] == 'amarelo')
    {
      $output.='<td style="background-color:#ffff80; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao']." = ".$linha['total'].'</td>';
    }
    elseif($linha['classificacao'] == 'verde')
    {
      $output.='<td style="background-color:#80ff80; text-align: center; padding: 8px; font-weight: bold;">'.$linha['classificacao']." = ".$linha['total'].'</td>';
    }
    $output.='</tr> ';

  }
  echo $output;
} catch (PDOException $e) {
  echo "<script>alert(\"Ocorreu um erro durante a listagem ".$e."\");</script>";
}
?>
  • Rafa I tried as you said, but he repeats several times the lines, and I wanted q appeared only 3 (one for each color), but thanks, it’s already half way

  • Ana, from a check, returned to me YELLOW = 1 GREEN = 5 RED = 3

  • I tried to summarize your bank, you’re using which column in COUNT?

  • I’m using question_id, it’s the table’s primary key, so I figured that’s what your "Cod" was! I’ll try again Rafa! Thank you

  • Hi. I saw better and had not noticed that the group would be (day, color), this way the query has to be : SELECT classificacao, data, Count(classificacao) from questionario GROUP by classificacao, data, this will count the number of greens/reds/yellows per pair (color, day).

  • vc need to separate by day and by color right? for example: day 06/06/18 YELLOW = 1 GREEN = 5 RED = 3 - day 07/06/18 YELLOW =5 GREEN = 2 RED = 1 ?

  • Rafa thank you very much, it worked, I had a while plus it was that! About the days, according to the example I showed in my question would be day 07-07-2018 green=1 (because there was only one qualificação that day, but of that I already treated, I used a between!

  • Legal Ana. Success in the project.

Show 3 more comments

1

)

It seems to me that the query does not agree with the following ifs, because the columns were aliased instead of keeping the column as "classification" and values "green", "yellow", "red".

select to use must be something like:

select classificacao, count(*) from questionario group by classificacao

for the following ifs to function

  • Ana, I tried the same now, using this query, but it is not giving in the same, it just repeats several times the word of color, does not return the number of times that the color appears

  • You want the number of colors per day?

  • Wees yes, but I’ve already taken care of that, so the only thing I need is for you to show how often "red" appears etc... The output would simply be 3 lines to say red:1 green: 4 yellow: 2

  • @Ana, the query I put here returns lines of type [color, #occurrences]&#Xa.

  • 1

    Ana thanks, the query is pretty much what I needed!

0

I guess this is it then @Ana:

$verificar=mysqli_query($link,"
    SELECT
        COUNT(CASE WHEN classificacao = 'verde' THEN classificacao END),
        COUNT(CASE WHEN classificacao = 'vermelho' THEN classificacao END),
        COUNT(CASE WHEN classificacao = 'amarelo' THEN classificacao END)
    FROM questionario        
");

Those COUNT() will count the separate colors, ai vc adapts to your current code.

  • Wees was what I had before in my code, only I edited the question, it just doesn’t show the result

-2

Hello

use

$verificar=mysqli_query($link,"
SELECT
    COUNT(CASE WHEN classificacao = 'verde' THEN classificacao END) as vermelho,
    COUNT(CASE WHEN classificacao = 'vermelho' THEN classificacao END) as vermelho,
    COUNT(CASE WHEN classificacao = 'amarelo' THEN classificacao END) as amarelo
FROM questionario        

");

and in the demonstration use for example

$row = mysqli_fetch_array($verificar);
echo $row['amarelo'];

so it will work.

  • Why did they vote bad? What mistake in my code? Since in mine it worked perfectly and I still helped!

Browser other questions tagged

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