1

I have a table which has the following structure:

+--------+---------+----------+
|Col1    | Col2      | Col3   |
+-----------------------------+
| BRA1   | QI        | QI     |
+--------+-----------+--------+
| BRA2   | Validated | QI     |
+-----------------------------+

I would like to higlight cell in red when it is equal to 'QI' and in green color when it is equal to 'Validated'.

This is what I did, but it doesn't work. Can any anyone could help to fix my script?

My php script:

<?php

$fields_num = mysqli_num_fields($result);
echo '<h1>Table:'.$tablename.'</h1>';
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($result);
echo '<td>'.$field[$i]->name.'</td>';
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
    if($cell=="QI"){
        //echo "<td>$cell</td>";
        echo '<td BGCOLOR="#ff0000">'.$cell.'</td>';}
        elseif ($cell=="Validated") {echo '<td BGCOLOR="#3f9a0e">'.$cell.'</td>';} //Green color BGCOLOR="#3f9a0e"

    echo "</tr>\n";
}
mysqli_free_result($result);


?>
1
  • Are you sure you wanna use foreach inside while ? Commented Feb 17, 2016 at 9:05

2 Answers 2

1

You need the style attribute and change the background-color

<td style="background-color: #ffffff">...</td>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code:

<?php
...
$fields_num = mysqli_num_fields($result);
echo '<h1>Table:'.$tablename.'</h1>';
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_fields($result);
echo '<td>'.$field[$i]->name.'</td>';
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
foreach($row as $cell) {
    if($cell=="QI"){

            echo '<td style="background:#ff0000">'.$cell.'</td>';

    } elseif ($cell=="Validated") {

            echo '<td style="background:#3f9a0e">'.$cell.'</td>';
    }
}

3 Comments

thanks for your script, but it colors cells but there is an issue. In output, contents are missing while I am displaying results. Plus first column, instead of displaying BRA1, BRA1, I have QI, Validated. So I was wondering if there isn't any issue with if and elseif statement.
Down vote? My answer is in line with your question. You could improve your question.
It works now, I forgot to put another condition when it is not Validated or QI. After the elseif, I added, else {echo "<td>$cell</td>";}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.