Colorize a column field according to parameters

Asked

Viewed 58 times

0

I have a table with five columns and two records. One of the fields in the record is a number (notaQualif), I need the field in which it is inserted to be painted green if it is greater than 75 and painted red if it is less than 75.

All data is stored in a database created in phpmyadmin, so it is necessary that when the user enters data, the notaQualif is checked and is soon painted green or red.

I leave here the code made so far that contemplates the table with the data.

<head><title>Ver registos</title></head>

<?php
    LigarBDfater(); //connect to the database


    // get results from database
    $result = mysql_query("SELECT * FROM qualificacao") or die(mysql_error());


    // display data in table
    echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
    echo "<table border='1' cellpadding='10'>";
    echo "<tr><th>ID Qualifica&ccedil;&atilde;o</th> <th>ID Fornecedor</th> <th>Nome Funcionario</th> <th>Nota Qualifica&ccedil;&atilde;o</th> <th>Data Qualifica&ccedil;&atilde;o</th></tr>";


    // loop through results of database query, displaying them in the table
    while($row = mysql_fetch_array($result)){

        // echo out the contents of each row into a table
        echo "<tr>";
        echo '<td>' . $row['id_qualific'] . '</td>';
        echo '<td>' . $row['id_fornecedor'] . '</td>';
        echo '<td>' . $row['nomeFuncionario'] . '</td>';
        echo '<td>' . $row['notaQualif'] . '</td>';
        echo '<td>' . $row['dataQualif'] . '</td>';
        echo "</tr>";
    }

    // close table>
    echo "</table>";
?>

<p align="left"><a href="novo_qualificacao.php">Adicionar um novo registo</a></p>

  • Put the code here, at least part, so I can help you.

  • There is a link in the "I leave it here" that redirects to the Dropbox with the code. Could visualize ?

  • As your question will be recorded here, it is good practice (if not a rule), paste the code into the body of the question.

  • Okok, I edited and now it’s in code

  • @crispypedro_ with code became very simple, check if the answer fits you.

1 answer

1


Make the following Change in your PHP:

if($row['notaQualif'] > 75) echo '<td class="bgVerde">' . $row['notaQualif'] . '</td>';
else echo '<td class="bgVermelho">' . $row['notaQualif'] . '</td>';

And in the styles apply:

td.bgVerde {
  background-color: #07CC36;
  color: #FFF;
}
td.bgVermelho {
  background-color: #c12536;
  color: #FFF;
}

Browser other questions tagged

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