PHP Auto-highlight cell depending on data from database

I am working on a PHP application and stumbled upon a problem on auto-highlighting.. I want to ask how to auto-highlight a cell of a table, whose data came from a database, after checking if the value is negative, 0, or positive, Red for negative, yellow for 0 and green for positive.

Please note that I have no experience regarding JavaScript or Ajax and rudimentary knowledge only on CSS. Thank you.

If needed, I can post any part of my code here.

Upvotes: 1

Views: 530

Answers (2)

Vijaya Pandey
Vijaya Pandey

Reputation: 4282

It might be helpful for you:

function getSampleStatus($sampleid){
    if($sampleid==1){
            $color="#007334";   
        }elseif($sampleid==2){
            $color="#3f96e8";
        }elseif($sampleid==3){
            $color="#ff9900";
        }elseif($sampleid==4){
            $color="#ff9770";
        }   
        else{
            $color="#ff0000";
        }
        $query='Select status from config where status_id='.$sampleid;
        $this->_db->setQuery( $query );
        $status =$this->_db->loadResult();
        return "<span style='color:".$color.";padding-left:200px;'>".$status."</span>";
    }

This function simply adds different colors depending on sample status.

For example:

  • For disapproved records, color red.
  • For Approved records, color green.
  • For Lab completed records, color yellow and so on..

Edit:

For Simplicity, try this:

$yourdatafromdatabase = 3; //which is either -ve, zero or positive

if($yourdatafromdatabase < 0){ 
    $color="#FF0000";   //red for less than zero
}elseif($yourdatafromdatabase== 0){
    $color="#FFFF00"; //yellow for zero
} 
else{
    $color="#00FF00"; //green for positive
}

echo "<span style=\"color: $color\"> <h1> Wow Color! </h1></span>";

?>

Working Demo:>>

Upvotes: 1

Ammar Hayder Khan
Ammar Hayder Khan

Reputation: 1335

just write css in table tag e.g

if ($value <0)
  {echo "<td bgcolor=\"#FF0000\">$value</td>";}

Upvotes: 0

Related Questions