Home > Software engineering >  Change div class value based on value in database
Change div class value based on value in database

Time:11-09

I have data from $row['nomor'] and I converted it into progress bar with bootstrap. My questions is, can you make, if 'nomor' is fall under 40 (for example) it will change the div class to div , so based on bootstrap the bar will change its color to red, and soon.

<?php
$hasil=mysqli_query($mysqli,"SELECT * from test");               
$row=mysqli_fetch_array($hasil);
?>
<tr>
<td><a href="pages/examples/invoice.html"><?php echo $row['nama']; ?></a></td>
<td><?php echo $row['nomor']; ?></td>
<td><span class="badge rounded-pill bg-danger">Realisasi</span></td>
<td><div class="progress progress-sm">
<div class="progress-bar progress-bar-striped bg-success progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $row['nomor']; ?>%"><?php echo $row['nomor']; ?>%</div></div></td>
</tr>

CodePudding user response:

You can do something like this.

<?php
$hasil=mysqli_query($mysqli,"SELECT * from test");               
$row=mysqli_fetch_array($hasil);

$progressBarClass = "bg-danger";

if ($row['nomor'] < 40){
  $progressBarClass = "bg-warning";
}
elseif($row['nomor'] < 80){
   $progressBarClass = "bg-primary";
}
else{
   $progressBarClass = "bg-success";
}
?>
<tr>
    <td><a href="pages/examples/invoice.html"><?php echo $row['nama']; ?></a></td>
    <td><?php echo $row['nomor']; ?></td>
    <td><span class="badge rounded-pill bg-danger">Realisasi</span></td>
    <td><div class="progress progress-sm">
        <div class="progress-bar progress-bar-striped <?php echo($progressBarClass); ?> progress-bar-animated" role="progressbar" aria-valuenow="75" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $row['nomor']; ?>%"><?php echo $row['nomor']; ?>%</div></div></td>
</tr>
  • Related