if statement inside an echo inside a mysql query result

Reply

Join Date: Oct 2009
Posts: 2
Reputation: sstewart is an unknown quantity at this point 
Solved Threads: 0
sstewart sstewart is offline Offline
Newbie Poster

if statement inside an echo inside a mysql query result

 
0
  #1
Oct 20th, 2009
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:

  1. $query = "SELECT
  2. dis_risk_RecordCheck,
  3. dis_risk_DisclosureForm
  4. FROM cus_discipline_riskmgmnt";
  5. $result = @mysql_query ($query); // Run the query.
  6. echo "<table class=cards width=100% border=1><tr>
  7. <td class=bluehdr>Record Check</center></td>
  8. <td class=bluehdr>Disclosure Form</center></td>
  9. </tr>";
  10. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  11. echo "<tr>
  12. <td class=cells>$row[1]</td>
  13. <td class=cells>$row[2]</td>
  14. </tr>";
  15. }
  16. 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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 523
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #2
Oct 20th, 2009
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-
  1. <?
  2. $query = "SELECT dis_risk_RecordCheck,dis_risk_DisclosureForm
  3. FROM cus_discipline_riskmgmnt";
  4. $result = @mysql_query ($query); // Run the query.
  5. echo "<table class=cards width=100% border=1><tr>
  6. <td class=bluehdr>Record Check</center></td>
  7. <td class=bluehdr>Disclosure Form</center></td>
  8. </tr>";
  9. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  10. echo "<tr>
  11. <td class=cells>$row[1]</td>
  12. <td class=cells style='backgorund-color:"; echo mysql_num_rows($result)==0?'red':'green'; echo ">$row[2]</td>
  13. </tr>";
  14. }
  15. echo "</table>";
  16.  
  17. ?>
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 818
Reputation: Airshow is on a distinguished road 
Solved Threads: 116
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark
 
0
  #3
Oct 20th, 2009
Sstewart,

Personally, I would do it like this:
  1. $query = "SELECT
  2. dis_risk_RecordCheck,
  3. dis_risk_DisclosureForm
  4. FROM cus_discipline_riskmgmnt";
  5. $result = @mysql_query ($query); // Run the query.
  6. $output = array();
  7. $output[] = "<table class=\"cards\" width=\"100%\" border=\"1\">";
  8. $output[] = "<tr>";
  9. $output[] = "<th>Record Check</th>";
  10. $output[] = "<th>Disclosure Form</th>";
  11. $output[] = "</tr>";
  12. while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  13. $result1 = $row[1];
  14. $result2 = $row[2];
  15. $output[] = "<tr>";
  16. $output[] = "<td class=\"cells $result1\">$result1</td>";
  17. $output[] = "<td class=\"cells $result2\">$result2</td>";
  18. $output[] = "</tr>";
  19. }
  20. $output[] = "</table>";
  21. 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
Last edited by Airshow; Oct 20th, 2009 at 8:52 am.
50% of the solution lies in accurately describing the problem!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: sstewart is an unknown quantity at this point 
Solved Threads: 0
sstewart sstewart is offline Offline
Newbie Poster
 
0
  #4
Oct 20th, 2009
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC