我想根据数据库中的值 php 更改 php 行文本

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

<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>

我想要实现的是当部门是计算机颜色应该是蓝色,什么时候是农业,颜色应该是绿色,什么时候是动力学颜色应该是红色等等

做一个函数,其中returns一个CSSclass一个字符串

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 '';
}

使 classes

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

调用函数

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

或者,制作 classes/colors

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

然后在 table 行上使用(有点乱)

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

如果要突出显示整行,请放在 tr,而不是 td