I want to change the php row text based on the value from the database with php

Viewed 40
<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                        <thead>
                                            <tr>
                                                <th>SL</th>
                                                <th>Name</th>
                                                <th>Department</th>
                                                <th>Course</th>
                                                <th>Unit</th>
                                                <th>Contact No</th>
                                                <th>Address</th>
                                                <th>Picture</th>
                                                <th>Date/Time</th>
                                            </tr>
                                        </thead>

                                        <tbody>
                                           <?php 
      $query=mysqli_query($db_con,'SELECT * FROM `student_info` ORDER BY `student_info`.`datetime` DESC;');
      $i=1;
      while ($result = mysqli_fetch_array($query)) { ?>
      <tr>
        <?php 
        echo '<td>'.$i.'</td>
        <td>'.ucwords($result['name']).'</td>
          <td>'.ucwords($result['department']).'</td>
          <td>'.ucwords($result['course']).'</td>
          <td>'.ucwords($result['unit']).'</td>
          <td>'.ucwords($result['pcontact']).'</td>
          <td>'.ucwords($result['address']).'</td>
          <td>'.ucwords($result['datetime']).'</td>
          <td><img class="rounded-circle avatar-xl" src="images/'.$result['photo'].'" ></td>';?>
          
      </tr>  
     <?php $i++;} ?>
                                        </tbody>
                                    </table>

All i want to achieve is when department is Computer color should be blue, when is Agric, color should be green, when is Kinetic color should be red etc.

1 Answers

Make a function, which returns a CSS class string

function departmentColor($value) {
  $value = strtolower($value);
  
  if ($value === 'computer') return 'bg-blue';
  if ($value === 'agric') return 'bg-green';
  if ($value === 'kinetic') return 'bg-red';
  return '';
}

Make the classes

.bg-blue{background-color:blue}
.bg-green{background-color:green}
.bg-red{background-color:red}

Call the function

<td class="<?= departmentColor($result['department']) ?>">

Or, make a map of classes/colors

$departmentColor = [
  'computer' => 'blue',
  'agric' => 'green',
  'kinetic' => 'red'
];

Then use on the table row (is bit messy)

<td style="<?= array_key_exists(strtolower($result['department']), $departmentColor) ? 'background-color:'.$departmentColor[strtolower($result['department'])] : '' ?>">

If you want the whole row highlighted, then place in tr, not the td.

Related