Jump to content

Newbie Conditional Formatting


getut

Recommended Posts

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

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>";

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";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.