Jump to content

Table Background Color based on Results from MySQL Table


rodin

Recommended Posts

Hi Everyone,

  I am trying to get the "Did User Agree" field on a database query to be colored Green for "Agreed" Red for "Disagreed" and Blue for "Agreed (2nd Time)" .. I have my PHP setup to do the query just fine as of right now with alternating column colors, just can't seem to get the colors to work as I am hoping for.  Can anyone help me get the Agreed? table's background color to the colors above? Any help is GREATLY appreciated! :)

 

<?php
error_reporting(7);

$db = mysql_connect("localhost","......",".....") or die("Problem connecting");
mysql_select_db(".....") or die("Problem selecting database");
$query = "SELECT * FROM rules ORDER BY username";
$result = mysql_query($query) or die ("Query failed");
//let's get the number of rows in our result so we can use it in a for loop
$numofrows = mysql_num_rows($result);
echo "<TABLE BORDER=\"1\">\n";
echo "<TR bgcolor=\"lightblue\"><TD>UserID</TD><TD>UserName</TD><TD>Agreed?</TD><TD>Date Agreed</TD></TR>\n";
for($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"yellow\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD>".$row['userid']."</TD><TD>".$row['username']."</TD><TD>".$row['diduseragree']."</TD><TD>".$row['dateagreed']."</TD>\n";
echo "</TR>\n";
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>

the easier solution to a for loop is

while($row = mysql_fetch_array($result)){

 

That will go through any way i'm assumign your accpet terms filed is bool in type so where it outputs say something like

if($row['Agreed'] >0){echo "bgcolor = \"green\"";} else{echo "bgcolor =\"red\"";} with the tags around it and it will work

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.