rama1 Posted March 30, 2011 Share Posted March 30, 2011 how to format only one specific row based on the value of one cell in a row? $result = mysql_query(select bal, etc. etc ..query ) while($row = mysql_fetch_assoc($result)) echo "<tr>"; echo "<td>" . $row['Account'] . "</td>"; echo "<td>" . $row['licence'] . "</td>"; echo "<td>" . $row['own'] . "</td>"; echo "<td>" . $row['bal'] . "</td>"; echo "</tr>"; Would appreciate help on how to format only the $row['bal'] output to red colour IF bal >5000 Thanks Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 Have a css style class that styles the td the way you want. I'm going to assume that the name of this class is ".red" echo ($row['bal'] > 5000) ? '' : ''; echo "{$row['bal']}"; If the first line looks a bit cryptic it's using the ternary operator, which is the equivalent of an if-then-else statement. I also included a little hint on how you can include an associative array element inside an interpolated string. A lot easier to read and maintain than having to concatenate all over the place. Quote Link to comment Share on other sites More sharing options...
rama1 Posted March 30, 2011 Author Share Posted March 30, 2011 Thank you gizmola for the swift help. I am new to PHP, will have a go using your suggestion & hint. Will report back. Quote Link to comment Share on other sites More sharing options...
rama1 Posted March 30, 2011 Author Share Posted March 30, 2011 Hi gizmola, tried the 'css style class' route with the following: <head> <style type="text/css"> td {color: red; } </style> </head> .... $result = mysql_query(select bal, etc. etc ..query ) while($row = mysql_fetch_assoc($result)) echo "<tr>"; echo "<td>" . $row['Account'] . "</td>"; echo "<td>" . $row['licence'] . "</td>"; echo "<td>" . $row['own'] . "</td>"; echo ($row['bal'] > 50000) ? '<td class="red">' : '<td>'; echo "{$row['bal']}</td>"; // echo "<td>" . $row['bal'] . "</td>"; echo "</tr>"; } .... The problem is that all the entries are formatted in red. Any way I can target the css to the $row['bal'] based on a condition. I will try the 'associative array element inside an interpolated string' later once I have figured out what it means (-; Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 This is not a php question at this point... it's become a css question. You didn't do what I instructed. <br /> td {color: red; }<br /> Is not creating a style class, it's saying "make all elements red. <br /> .red {<br /> color: red;<br /> }<br /> Quote Link to comment Share on other sites More sharing options...
rama1 Posted March 30, 2011 Author Share Posted March 30, 2011 YOU R A STAR & a very helpful person. It just worked . Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.