| | |
if statement inside an echo inside a mysql query result
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 2
Reputation:
Solved Threads: 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:
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
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:
PHP Syntax (Toggle Plain Text)
$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
•
•
Join Date: Sep 2009
Posts: 523
Reputation:
Solved Threads: 61
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-
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-
PHP Syntax (Toggle Plain Text)
<? $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
#3 Oct 20th, 2009
Sstewart,
Personally, I would do it like this:
NOTES:
Personally, I would do it like this:
PHP Syntax (Toggle Plain Text)
$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);
- 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
.yesand.no, thereby controlling appearance in your style sheet rather than inline styles. - Off the cuff - not tested
Last edited by Airshow; Oct 20th, 2009 at 8:52 am.
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- HOw to write MYsql Query in cakephp (PHP)
- How do i echo a variable in a php mysql query ? (PHP)
- javascript and a MySQL query (JavaScript / DHTML / AJAX)
- Mysql query result (PHP)
- external links in php + mysql query code. (PHP)
- Supplied argument is not a valid MySQL result resource (PHP)
- mysql query error(need help) (PHP)
- Query class problem (PHP)
Other Threads in the PHP Forum
- Previous Thread: find county using ip address
- Next Thread: please help............
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





