Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Cell Formatting 1

Status
Not open for further replies.

chandler

MIS
Dec 10, 2000
66
0
0
US
Is it possible to do conditional cell formatting in HTML tables similar to Excel? I'm trying to change the cell's background color depending on the value in the cell. The table cell values will come from PHP returned values.

Chandler
I ran over my dogma with karma!
 
HTML is a markup language and has no logic. So you cannot apply any logical test to the markup. However, since you said you will be using PHP to populate the values, you can do that there. Simply create a special class for the special formatting and when the number falls in that category, apply the class to the table cell. More in-depth answers should be available to you in the PHP forum (forum434)
 
I agree with Vragabond, using PHP will make it possible, I've not done conditional formatting on a cell before, but i have got a piece of code that will place an image (in my case iether a green or red flag) next to the cell dependant on the property value of a cell.

If you are interested in the code for then let me know.

Rob
 
Rob, I'm very interested in that code because that is exactly something I was going to implement for another part of the page. Thanks.

Chandler
I ran over my dogma with karma!
 
Here we are buddy, i think this is all the code you should need for it, its a simple if/else statement thats just attached to an image. This is ripped right out of one of my projects so they'll be the odd bit of gubbins mixed in but should make sense.

Basicly it starts with your standard MySQL query on my data, selecting the fields to be presented, the key field for this demonstration is the "withdrawldate", basicly the concept was if a date was present in this field then we flagged the candidate as a red flag.

Next is the IF statement, which basicly says IF the withdraeldate field is empty then $image is greenflag.gif if it is anything ELSE then $image is redflag.gif.

Further down you then have the PRINT statement which just dumps $image after the candidates name is listed.

Hope that makes sense, i'm sure you can adapt it to your requirements, if you need any help them dont hesitate to ask.

Rob

Code:
$result = mysql_query("SELECT surname,givenname,id,withdrawaldate FROM students WHERE cid=$companyresults[0] ORDER BY surname"); 
      $studentcounter = 0; 
      while($book_row = mysql_fetch_array($result)) 
      { 
        $studentcounter++; 
         
        if(empty($book_row['withdrawaldate'])) 
            { 
                $image = "<img src='[URL unfurl="true"]http://www.not-telling-you-my-domain.org/greenflag.gif'[/URL] />"; 
            } 
            else
            { 
                $image = "<img src='[URL unfurl="true"]http://www.not-telling-you-my-domain.org/redflag.gif'[/URL] />"; 
            } 
               
        print "<LI><A class=\"student\" HREF=\"student.php?do=view&sid=$book_row[2]&cid=$companyresults[0]\">$book_row[0], $book_row[1]</A>$image<BR>\n"; 
      }
 
Thanks Rob. That'll do nicely. Muchas Gracias.

Chandler
I ran over my dogma with karma!
 
Your welcome Chandler,

Vragabond, the only reason the post is sat here is because the origional question was posed on the assumption that Chandler would be able to achieve the desired result using HTML, turns out he couldn't.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top