getut Posted November 20, 2012 Share Posted November 20, 2012 Can someone help me with some conditional formatting? I'm an extreme newbie and this is the first code I have ever done, so I apologize for the simplicity. I've googled and couldn't understand what I was reading enough to get it working in the context that I need it to. Here is the php on apache accessing a MS SQL Server. The code as it is works but I want the output text to be bigger and I want a green background unless the returned value is 0, in which case I want the background to be red. Can anyone help? <?php $szQry = "SELECT COUNT (*) From dbo.MHC_LOCATION Where location_status = 'Empty' and locked_flag = 'Unlocked'"; $szDBConn = mssql_connect("madeupservername","bogususer","boguspassword"); mssql_select_db("NGK", $szDBConn); $saResults = mssql_query($szQry, $szDBConn); while($obResults = mssql_fetch_row($saResults)) { // echo $obResults[0]." ".$obResults[1]." Empty Spaces Remaining"; echo $obResults[0]." Empty Spaces Remaining"; } mssql_close($szDBConn); Link to comment https://forums.phpfreaks.com/topic/270956-newbie-conditional-formatting/ Share on other sites More sharing options...
requinix Posted November 20, 2012 Share Posted November 20, 2012 Start with the most obvious method: if (zero open spaces) { print message with a red background } else { print message with a green background } if($obResults[0] == 0) { echo "<whatever you do to make it green>0 Empty Spaces Remaining</whatever>"; } else { echo "<whatever...>" . $obResults[0] . " Empty Spaces Remaining</whatever>"; } After that works you can try to simplify it. Like by noticing that the only thing changing is the opening . if($obResults[0] == 0) { echo "<whatever you do to make it green>"; } else { echo "<whatever...>"; } echo $obResults[0]." Empty Spaces Remaining</whatever>"; Link to comment https://forums.phpfreaks.com/topic/270956-newbie-conditional-formatting/#findComment-1393928 Share on other sites More sharing options...
Psycho Posted November 21, 2012 Share Posted November 21, 2012 I prefer to use the logic to set a style property or class in these situations. If there are only two options then I prefer the ternary operator to an if/else: while($obResults = mssql_fetch_row($saResults)) { $color = ($obResults[0] != '0') ? 'green' : 'red'; echo "<span style='color:{$color};'>{$obResults[0]} Empty Spaces Remaining</span><br>\n"; } Link to comment https://forums.phpfreaks.com/topic/270956-newbie-conditional-formatting/#findComment-1393945 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.