Jump to content

Color-code php export to html


cmattoon

Recommended Posts

Hey all,

I'm familiar with HTML/CSS, but brand new to PHP and such. I have built a MySQL database, and one of my queries brings up the following information

 

Timestamp, ID#, Gender, Code #

 

ID# can be just about anything (VARCHAR(50))... doesn't matter here..

Gender is (0)female, (1)male

Code #'s are 0-4 (inclusive).

 

Currently, I have a script that pulls the information and puts it into an HTML table. I want to highlight the rows (or even just the "code#" cells) by what the code is:

 

if Code#=0, Black background, white text

if Code#=1, Red background, white text

if Code#=2, Yellow background, black text

if Code#=3, Green background, white text

if Code#=4, Blue background, white text

 

I would imagine I could copy this code to highlight "gender" cells pink/blue. I just have no idea how to do this (i'd imagine an IF-THEN statement, but I'm having a hard time for some reason.

 

Any ideas?

 

Is it possible to sort this data by any of the above values (Time, ID, Gender, Code)??

 

 

Thanks!!

 

Link to comment
Share on other sites

I hate to sound lazy, but I literally started playing with PHP last night. I'm catching on, but I usually end up messing up my script when I try to do stuff. I've been borrowing from script libraries to learn, but just plain not sure how to go about this one....like I said, i *think* it can be done with an if-then, but not sure how to insert it into the code, or if it's even the best way.

 

 

Link to comment
Share on other sites

<?php

include("access_db.inc");

 

$sql = mysql_query("SELECT * FROM patients");

 

 

echo "<table border=\"1\"><tr><td> TIMESTAMP </td><td>Tag Number</td><td>SEX</td><td>Tag Color</td><td>Location #</td></tr>";

 

while ($row = mysql_fetch_row($sql)) {

echo "<tr><td>$row[8]</td><td>$row[0]</td><td>$row[5]</td><td>$row[11]</td><td>$row[17]</td></tr />";

}

 

?>

 

 

This is what I have so far....

Link to comment
Share on other sites

You should use mysql_fetch_assoc on the resource so you can call $row['gender'] if need be instead of by index. Your colours can be decided by a <span>/td tag handled obviously in each iteration of the loop.

 

while ($row = mysql_fetch_assoc($sql)) {
  if($row['gender'] == 1) {
     $color="pink";
  } else {
     $color="blue";
  }
  echo "<tr><td style='background-color: $color;'>{$row['gender']}</td>...";
}

 

Of course this is just a quick sample, How you implement it depends on what you simply wish.

Link to comment
Share on other sites

Thanks!

 

When using the ' character... <td background style='color:red'>

 

is it necessary to do the \' thing like you would for <color=\"red\">?

 

Single quotes can freely be used in double, And vice versa. You only need to escape them if they're going to be closing the opening quote.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.