0

Hello,

I have a piece of code that pulls data from a mysql database. I am displaying the results in a table but want to format the background of certain cells based on the query results (ie red for no green for yes).

Here is my code:

$query = "SELECT 
			dis_risk_RecordCheck,
			dis_risk_DisclosureForm
		FROM cus_discipline_riskmgmnt";
$result = @mysql_query ($query); // Run the query.
   echo "<table class=cards width=100% border=1><tr>
	  <td class=bluehdr>Record Check</center></td>
	  <td class=bluehdr>Disclosure Form</center></td>
	  </tr>";
   while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   echo "<tr>
          <td class=cells>$row[1]</td>
          <td class=cells>$row[2]</td>
		</tr>";
    }
echo "</table>";

In the above code I would like to have the row[2] results show a green background if the query result is yes and a red background if the result is no.

Any help is appreciated!

Thanks in advance!

ss

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by sstewart
0

what do you mean by the result 'yes' or 'no'?
You mean number of rows returned zero then result is no and when there are some rows, you mean the result as 'yes'.
If thats the case, code below-

<?
$query = "SELECT dis_risk_RecordCheck,dis_risk_DisclosureForm
		FROM cus_discipline_riskmgmnt";
$result = @mysql_query ($query); // Run the query.
   echo "<table class=cards width=100% border=1><tr>
	  <td class=bluehdr>Record Check</center></td>
	  <td class=bluehdr>Disclosure Form</center></td>
	  </tr>";
   while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   echo "<tr>
          <td class=cells>$row[1]</td>
          <td class=cells style='backgorund-color:"; echo mysql_num_rows($result)==0?'red':'green'; echo ">$row[2]</td>
		</tr>";
    }
echo "</table>";

?>
0

Sstewart,

Personally, I would do it like this:

$query = "SELECT 
			dis_risk_RecordCheck,
			dis_risk_DisclosureForm
		FROM cus_discipline_riskmgmnt";
$result = @mysql_query ($query); // Run the query.
$output = array();
$output[] = "<table class=\"cards\" width=\"100%\" border=\"1\">";
$output[] = "<tr>";
$output[] = "<th>Record Check</th>";
$output[] = "<th>Disclosure Form</th>";
$output[] = "</tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$result1 = $row[1];
	$result2 = $row[2];
	$output[] = "<tr>";
	$output[] = "<td class=\"cells $result1\">$result1</td>";
	$output[] = "<td class=\"cells $result2\">$result2</td>";
	$output[] = "</tr>";
}
$output[] = "</table>";
echo implode("\n", $output);

NOTES:

  • Build output string in an array, then join with implode to better control the format of generated HTML (useful when inspecting with view source).
  • Escaped double quotes around all HTML attributes.
  • Use <th> for table header cells, which can be separately styled in css.
  • Create styles for .yes and .no , thereby controlling appearance in your style sheet rather than inline styles.
  • Off the cuff - not tested

Airshow

Edited by Airshow: n/a

0

Hi all,

Thank you for the posts! Just to clarify, the yes / no are values stored in the database for the two fields I'm looking at (Records / Disclosure). I'm not sure if that makes a difference...???

I'll try the two options noted above today!

ss

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.