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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.