首页 > 解决方案 > 单元格的所有背景颜色都相同

问题描述

我正在为学生网创建学期地图。如果正在学习课程,则单元格颜色应为黄色。如果选课,应该是绿色的。如果课程尚未上课,单元格颜色将为红色。

我正在测试 id 为“1”的学生,在“course_status”表中有一个名为“course_status”的列,其中只有“pending”、“in_progress”和“done”。

$sql = "SELECT * FROM course_status WHERE student_id = 1";
$retval = mysqli_query( $link, $sql );
$row=mysqli_fetch_array($retval);
if($row['course_status']== 'done') {$status_color = "green";}
if($row['course_status']== 'in_progress') {$status_color= "yellow";}
if($row['course_status']== 'pending') {$status_color= "red";}
<td style="background-color: <?php echo $status_color; ?>;">CSCI 185 
<td style="background-color: <?php echo $status_color; ?>;">CSCI 385 
<td style="background-color: <?php echo $status_color; ?>;">CSCI 485 

尽管学生尚未参加 CSCI 385&485,但所有单元格的背景颜色均为绿色。我只希望 CSCI 185 有一个绿色背景,因为它已经被采用了。

标签: phphtmlmysqlsql

解决方案


尝试如下:

if($row['course_status']== 'done') {

<td style="background-color:green">something</td>//bg-color will be green

}else if($row['course_status']== 'in_progress'){

<td style="background-color:yellow">something</td>//bg-color will be yellow

} else {
<td style="background-color:red">something</td> //bg-color will be red

} 

推荐阅读