How to color a particular column in a table using PHP

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
chipotle
Forum Newbie
Posts: 1
Joined: Wed Oct 05, 2005 2:27 pm

How to color a particular column in a table using PHP

Post by chipotle »

I have a table displayed that is retreived from the database. Based on a value in a particular column I need to change the color of that display. eg. column status can hold the values new, approved, rejected etc.. I want to color that column based on the values. If it is new then green, approved then blue etc...


Please help :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look at the value in the current record, emit either a color via a div/span or set the table cell to a particular class.. a basic (untested) example

Code: Select all

// assume $row is an associative array of the record's details
switch(strtolower($row['status'])) {
case 'new':
  $cell = 'green';
break;

case 'approved':
  $cell = 'blue';
break;

case 'rejected':
  $cell = 'red';
break;
}

echo '<td class="'.$cell.'">'.$row['status'].'</td>';

// emit the rest of the record's junk
Post Reply