Change Row Colors PHP SQL

Hi Guys

Please give me some advice, i have a few ideas but not working so well …

I have a database with defect type, urgency, description etc.

i would like to show all defects on the page but if it is urgent it must make the row red, if not urgent green and so on. getting the data and displaying it i got right just struggling with the color coding

Thanks in advance

Use if statements for this. Just save the rows as their urgent level. Then based on that level, use if statements and then apply the CSS according to the urgent level.

1 Like

Yes, something like this should work

while ($row ...
$class_value = ''; 
if ($row['urgency'] === "urgent") { 
  $class_value = "urgent";
.....
<tr class="<?php echo $class_value; ?>"> 
// depending on how you're putting together the output 

Then in your CSS

tr.urgent td { 
  background-color: #F00; 
}
3 Likes

awesome, i will try this, i actually was on this path, but this clears it up more

thank you

1 Like

displaydefect.php (2.7 KB)
I think i`m almost there just very strange if someone can help me, sure it is something stupid.

It actually colors the row RED that is Urgent but only every second one … and it does not color my second if green. so basically only Urgent is showing and Not Urgent is not showing.

had to upload as it posted as garbish

Apologies for my previous garbish posts,

the code:

<html>
<link rel="stylesheet" type="text/css" href="disp.css">

<head>
    <meta charset="utf-8" />
    <html lang="en">

    <body>
        <form>

            <table class="data-table">
                <thead>
                    <?php
include('dbconnection.php');

  
?>

                        <thead>
                            <tr>
                                <th>Defect No</th>
                                <th>Defect Type</th>
                                <th>Defect Sub Type</th>
                                <th>Urgency</th>
                                <th>Description</th>
                                <th>Door Number</th>
                                <th>Area</th>
                                <th>Reported By</th>
                                <th>Client Details</th>
                                <th>Contact Details</th>
                                <th>Date Reported</th>


                            </tr>
                        </thead>
                    
<?php
            
$query = "SELECT * FROM defects "; 
$result = mysqli_query($link6,$query);
                      
$no = 1;  
                        
while($row=mysqli_fetch_array($result)){

    if($row["urgency"]=== "Urgent"){
                    
                   
echo "<tr>";
echo "<td class='urgent'> $no</td>";
echo "<td class='urgent'>{$row["type"]} </td>";
echo "<td class='urgent'>{$row["dtype"]}</td>";
echo "<td class='urgent'>{$row["urgency"]}</td>";
echo "<td class='urgent'>{$row["descr"]}</td>";
echo "<td class='urgent'>{$row["doornum"]}</td>";
echo "<td class='urgent'>{$row["area"]}</td>";
echo "<td class='urgent'>{$row["reportedby"]}</td>";
echo "<td class='urgent'>{$row["namesurname"]}</td>";
echo "<td class='urgent'>{$row["contact"]}</td>";
echo "<td class='urgent'>{$row["date"]}</td>";

        
echo "</tr>";
          
        
    
}
    
if($row=["urgency"]==="Not Urgent"){
    
    echo "<tr>";
echo "<td class='nurgent'> $no</td>";
echo "<td class='nurgent'>{$row["type"]} </td>";
echo "<td class='nurgent'>{$row["dtype"]}</td>";
echo "<td class='nurgent'>{$row["urgency"]}</td>";
echo "<td class='nurgent'>{$row["descr"]}</td>";
echo "<td class='nurgent'>{$row["doornum"]}</td>";
echo "<td class='nurgent'>{$row["area"]}</td>";
echo "<td class='nurgent'>{$row["reportedby"]}</td>";
echo "<td class='nurgent'>{$row["namesurname"]}</td>";
echo "<td class='nurgent'>{$row["contact"]}</td>";
echo "<td class='nurgent'>{$row["date"]}</td>";

        
echo "</tr>";
    
  $no ++;
    
   }
    
}



?>
                </thead>

            </table>



        </form>
    </body>

    </html>

Two things for me:

I don’t know why you have the second if() clause - can the urgency be anything other than “urgent” or “not urgent”? If it cannot, the second if() is redundant. ETA - oh, and typo:

if($row=["urgency"]==="Not Urgent"){

As you use CSS to style the output, you probably need to show the CSS for anyone to help much.

Do you mean that it does not colour the “not urgent” rows, or that they do not show at all? Typo above would probably cause the latter.

Only two lines of CSS, still very very early design

td.urgent {
    
    background-color:red;
}
td.nurgent{
    background-color: green;
}

Not Showing at all, and only coloring every second urgent,

i will check now what you have helped me with.

thanks for your time

It can Be Urgent, Critical, Very Urgent, Not Urgent

so either 4 If`s or a case ?

Ok so the spelling was wrong indeed :slight_smile:

it now shows the red Urgent, and Green Not Urgent but still only making every second line a color very sporadic

i suspect i know my IFs are the problem but cant see past my own logic :frowning:

I would use a switch() construction for that. I would also change the database so that instead of using text for the status, I would use a code 1, 2, 3 or 4.

One easy way to simplify the code here would be to create an associative array where the index is the possible values for the status, and the data for each is your class name.

$classname = array("urgent" => "urgent", "not urgent" => "nurgent", ... and so on

then you can do away with all the ifs and cases and just use something like

// at the start of each iteration of the while() loop
if (exist($classname[$row['urgency']])) { 
  $cls = $classname[$row['urgency']];
  }
else {
  $cls = "unknown"; // or however you want to style invalid codes
  }
// and then for each column do something like:
echo "<td class='" . $cls . "'>{$row["type"]} </td>";

You could do a “view source” on your page and see if that gives any clues. Post the html here if it doesn’t. Is “class=data-table” adding anything like an nth-child rule?

I would make this much simpler by just doing

<td class="urgency-<?php echo strtolower(str_replace(' ', '-', $row['urgency'])); ?>">

So urgent rows will get class urgency-urgent, non urgent will get urgency-not-urgent and you can then just style those. No if, no switch, just straight forward mapping to CSS classes.

Added bonus is if you ever want a color for another urgency or even if you add a completely new urgency and you want a color for that you can just add the CSS code and you’re done, no changes needed to the PHP code at all.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.