background color in input with highest value

Asked

Viewed 70 times

0

Return a query in a table that is mounted this way in php:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {
$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['AvaliacaoGlobal'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['CGenericas1'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['CEspecificas1'].'</td>';
}

To find the highest value in the first column do this if inside the while:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

if ($maior < $teste = $rows_cursos['CGenericas1'])
   $maior = $teste = $rows_cursos['CGenericas1'];

...
}

But now I intend that by finding the highest value in the first column, that puts color at the bottom of that cell, with the highest number in blue.

Anderson Carlos Woss, I’m trying to implement your suggestion, but I haven’t been able to get the desired result yet. After the code to find the highest value I am trying to compare as follows:

if ($maior == $teste){
        $cormaxGenericas = 'bgcolor=blue';
        }else{
        $cormaxGenericas = 'bgcolor=white';
    }

the result is that you are putting blue background color in the first 3 cells, where you should only put blue color in the third that is the highest value:

inserir a descrição da imagem aqui

Complete code:

$maior=0;
while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {
if ($maior < $teste = $rows_cursos['CGenericas1'])
   $maior = $teste = $rows_cursos['CGenericas1'];

    if ($maior == $rows_cursos['CGenericas1']){
        $cormaxGenericas = 'bgcolor=blue';
        }else{
        $cormaxGenericas = 'bgcolor=white';
    }
$tabela2 .= '<td style="text-align: center; font: 10pt Arial;"'.$cormaxGenericas.'>'.$rows_cursos['CGenericas1'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['CEspecificas1'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['AvaliacaoGlobal1'].'</td>';
}

Solution I found: I created a while to find only the greatest value:

while($rows_cursos1 = mysqli_fetch_array($resultado_cursos1)) {
    if ($maior < $teste = $rows_cursos1['CGenericas1'])
    $maior = $teste = $rows_cursos1['CGenericas1'];
}

Then I created the while where I compare the highest value with the value within this new while:

while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

    if ($maior == $rows_cursos['CGenericas1']){
        $cormaxGenericas = 'bgcolor=blue';
        }else{
        $cormaxGenericas = 'bgcolor=white';
    }
$tabela2 .= '<td style="text-align: center; font: 10pt Arial;"'.$cormaxGenericas.'>'.$rows_cursos['CGenericas1'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['CEspecificas1'].'</td>';

$tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['AvaliacaoGlobal1'].'</td>';
}

Upshot:

inserir a descrição da imagem aqui

  • Inside your repeating loop that mounts the table, you can compare the current value with the higher value you found earlier. If they are equal, set the background to blue. Want to try?

  • and if there are two or more equal values?

  • @Leo Caracciolo, if you have two equal values you have to put both in blue background

  • @Anderson Carlos Woss, I edited the question to show how I am trying to implement your suggestion, but it is not yet as I intend

  • You are finding the biggest in the same loop you are displaying?

  • @Anderson Carlos Woss, I edited the question with the full code. Yes, I’m showing in the same loop where I’m finding the highest value

  • So this is a problem; you have how to know which will be the largest number without evaluating all the samples before.

  • To clarify, you need to highlight the highest value in all columns?

  • Yes, I need to know the highest value in these three columns and put blue background color, to identify the collaborator with the best performance in each of them

  • @Anderson Carlos Woss, I managed to solve the problem, but I do not know if it is the best practice, I will edit the question with the solution I found

  • You shouldn’t publish your solution within your question, you better answer your own question.

Show 6 more comments

2 answers

1

SELECT max() - returns the highest value of the specified column.

Inside the while make the comparison with this returned value

    $result_max = mysqli_query($conn,"SELECT max(CGenericas1) AS max_page FROM AvaliacaoDesempenho");
    $rows = mysqli_fetch_array($result_max);
    $maximo =  $rows["max_page"];

    $resultado_cursos = mysqli_query($conn, "SELECT * FROM AvaliacaoDesempenho");

    while($rows_cursos = mysqli_fetch_array($resultado_cursos)) {

        if($rows_cursos['CGenericas1']==$maximo){
            $corGenericas = 'bgcolor=blue';
        }else{
            $corGenericas = '';
        }

        $tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['AvaliacaoGlobal'].'</td>';

        $tabela2 .= '<td style="text-align: center; font: 10pt Arial;"'.$corGenericas.'>'.$rows_cursos['CGenericas1'].'</td>';

        $tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$rows_cursos['CEspecificas1'].'</td>';

    }
  • thanks for the tip regarding answering the solution within the question. The solution I found is not a good practice? It is better the one you presented?

  • @Bruno, the answer is dated in this post https://answall.com/questions/346459/mysql-e-php-performance-entre-while-e-select-max If you understand explain to me :-)

  • @Sam, to, tell me

1

Make a 'for' for every need:

1) Define index of higher value

$indice = 0;

for($i = 0; $i <= mysql_num_rows($resultado_cursos); $i++) {
     if ($maior < $teste = $resultado_cursos[$i]['CGenericas1'])
           $maior = $teste = $resultado_cursos[$i]['CGenericas1'];

           $indice = $i;
     }
}

2) Check the index:

for($i = 0; $i <= mysql_num_rows($resultado_cursos); $i++) {
   if ($i == $indice) {
       // Com Cor

       $tabela2 .= '<td style="text-align: center; font: 10pt Arial; background-color: ' . $cor . ';">'.$resultado_cursos[$i]['AvaliacaoGlobal'].'</td>';
   } else {
      // Sem Cor

       $tabela2 .= '<td style="text-align: center; font: 10pt Arial;">'.$resultado_cursos[$i]['AvaliacaoGlobal'].'</td>';
   }
}
  • See if it gets closer now!

Browser other questions tagged

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