How to customize returned MYSQL dynamic table row in PHP?

Viewed 33

I have the following code, that creates a dynamic table with dynamic thead and tbody, the data is from a MySQL pivot table but displayed in a through php

 while($row = $res->fetch_row())
      {
          echo "<tr>";
          foreach($row as $cell) {
           // dd($row);
            if ($cell === NULL) { $cell = '-'; }
         
            echo "<td>$cell</td>";
          }
          echo "</tr>\n";
      }

I want to be able to return specific values for example the current result being returned is:

Subject Mark Comment
English Language 43 Good work
English Literature 59 Good

but I want to be able to via php make the mark color red when the student has recieved 50%, how can I doo that in the $cell variable?

1 Answers

Use an if statement to add a class to the row, and then use CSS to make that show red.

while($row = $res->fetch_row())
{
    if ($row['mark'] <= 50) {
        $class = 'class="red"';
    } else {
        $class = '';
    }
    echo "<tr $class>";
    foreach($row as $cell) {
        // dd($row);
        if ($cell === NULL) { $cell = '-'; }
         
        echo "<td>$cell</td>";
    }
    echo "</tr>\n";
}
Related