Search code examples
phpjquerydatatablecolorsrow

Datatable change row color depend sql value


How can i change color row color depend "status" value? For Example status "OK" Color will be green etc.

My insert method is Ajax.

I tried too much method but didn't change the color.

Thanks for help.

My code part is:

<table id="employee_data" class="table table-striped table-bordered">  
 

     <thead>  
           <tr>  
                <center><td>ID</td></center>  
                <center><td>Date</td></center>  
                <center><td>Brand</td></center>
                <center><td>Product</td></center>
                <center><td>Serial No</td></center>
                <center><td>Company</td></center> 
                <center><td>Status</td></center>
                <center><td>Worker</td></center>
                <center><td>Explain</td></center>
                <center><td></td></center>
                
                
           </tr>  
      </thead>  
      <?php  
      while($row = mysqli_fetch_array($result))  
      { 
          
          ?>  
            <center><tr>  
                <center><td><p><font color="black"><?php echo $row["id"]; ?></td></center>
                <td><p><font color="black"><center><?php echo $row["date"]; ?></center></td>
                <td><p><font color="black"><center><?php echo $row["brand"]; ?></center></td>
                <td><p><font color="black"><center><?php echo $row["product"]; ?></center></td>
                <td ><p><font color="black"><center><?php echo  $row["serialno"]; ?></center></td>
                <td><p><font color="black"><center><?php echo  $row["company"]; ?></center></td>
                
                <td><p><font color="black"><center><?php echo $row["status"]; ?></center></td>
                <td><p><font color="black"><center><?php echo  $row["worker"]; ?></center></td>
                <td><p><font color="black"><center><?php echo  $row["explain"]; ?></center></td>
               
                <td><input type="button" name="edit" value="Edit" id="<?php echo $row["id"]; ?>" class="btn btn-info btn-xs edit_data" /> <p></p> <?php echo ' <a href="print.php?id='.$row['id'].'" target="_blank" onclick="return uyari();"><button type="button"  class="btn btn-info btn-xs">Print</button> </a>  ';?></td> 
                
                          
           </tr>  </center>
           <?php  
           }  
           ?>  
           
</table>  

Solution

  • You can create a php variable for the color, and do something like this.

    <?php
    $color = "black";
    
    if ($row["status"] == "OK") {
        $color = "red";
    }
    ?>
    

    And then:

    <center><td><p><font color="<?php $color ?>"><?php echo $row["id"]; ?></td></center>
    <td><p><font color="<?php $color ?>"><center><?php echo $row["date"]; ?></center></td>
    ...