Skip to content
Advertisement

Change text color based on condition

is there an example to change color of the text based on conditions? for example i want to make condition when $hasil>=80 it will turn green color, $hasil>=70 yellow, and below that is red color.

here’s my logic code

while ($data = mysqli_fetch_assoc($result))
{
    $n1 = ($data["nilai_output"]) * 0.7;
    $n2 = ($data["nilai_atasan"]) * 0.1;
    $n3 = ($data["nilai_learning"]) * 0.1;
    $n4 = ($data["nilai_kedisiplinan"]) * 0.05;
    $n5 = ($data["nilai_5r"]) * 0.05;

    $hasil = ($n1 + $n2 + $n3 + $n4 + $n5);

    if ($hasil >= 95)
    {
        $grade = 1.25;
        $ikk = "istemewa";
    }
    elseif ($hasil >= 90)
    {
        $grade = 1.10;
        $ikk = "Sangat Memuaskan";
    }
    elseif ($hasil >= 85)
    {
        $grade = 1.00;
        $ikk = "Memuaskan";
    }
    elseif ($hasil >= 80)
    {
        $grade = 0.90;
        $ikk = "Cukup Memuaskan";
    }
    elseif ($hasil >= 75)
    {
        $grade = 0.75;
        $ikk = "Memadai";
    }
    elseif ($hasil >= 70)
    {
        $grade = 0.50;
        $ikk = "Kurang Memadai";
    }
    elseif ($hasil >= 1)
    {
        $grade = 0.25;
        $ikk = "Tidak Memadai";
    }
    else
    {
        $ikk = "Tidak Berkontribusi";
    }

    $no++;   

and here’s my table column

  <td class="font-weight-bold text-danger"><?php echo $hasil;?></td>
  <td class="font-weight-bold text-danger"><?php echo $grade;?></td>
  <td class="font-weight-bold text-danger"><?php echo $ikk;?></td>

i want that those three column is using color condition thanks before

Advertisement

Answer

Try like this

if ($hasil >= 80) {
  $grade = 0.90;
  $color = 'green';
  $ikk = "Cukup Memuaskan";
}elseif($hasil >= 70 && $hasil < 80 ) {
  $grade = 0.75;
  $color = 'yellow';
  $ikk = "Memadai";
}elseif($hasil >= 1 && $hasil < 70 ) {
  $grade = 0.50;
  $color = 'red';
  $ikk = "Kurang Memadai";
}else{
  $ikk = "Tidak Berkontribusi";
}


<td class="font-weight-bold text-danger" style="color:<?=$color;?>"><?php echo $hasil;?></td>

I hope this is helps you!

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement