conditional webpage text coloring

I am trying to modify an existing web page we use at our facility to monitor mill performance. I thought that what I wanted to do was simple but is proving to be a pain. If $result is greater than 15, for a specific parameter, I want the displayed value text on the webpage to change colors. <15 = green. >= 15 = red.
The site author didn’t use css so I am trying to incorporate into the echo statement. See my changes in RED below please:

<?php $db = odbc_connect("OEE NEW", "xxx", "xxxx") or die ("could not connect
"); // db connector $stmt = "SELECT TOP 1 * FROM NDT_OEE ORDER BY ID DESC"; // sql select cmd $result = odbc_exec($db, $stmt); if ($result == FALSE) die ("could not execute statement $stmt
"); // error message echo "NDT"; while (odbc_fetch_row($result)){ echo ""; echo "
TIME AVAILABILITY % PERFORMANCE % OEE % DOWNTIME PROD. COUNT PROVEUP %
" . odbc_result($result, "DATE_TIME"); echo " " . odbc_result($result, "REALTIME_AVAILABILITY"); echo " " . odbc_result($result, "REALTIME_PERFORMANCE"); echo " " . odbc_result($result, "REALTIME_OEE"); echo " " . odbc_result($result, "REALTIME_DOWNTIME"); echo " " . odbc_result($result, "REALTIME_PRODUCTION_COUNT"); [u][i][b]if ($result["REALTIME_PROVEUP_RATE"] < "15"){ echo " " . odbc_result($result, "REALTIME_PROVEUP_RATE"); } else { echo " " . odbc_result($result, "REALTIME_PROVEUP_RATE"); }}[/b][/i][/u] odbc_free_result($result); odbc_close($db); ?>

It is querying from SQL and displays the data perfectly. The displayed value is always ‘green’ now and I cant seem to see the problem.

Any advice or help would be greatly appreciated.

  1. Have you tried the opposite?
    Why are you quoting the number? Is it a string or numeric?

[php]if ($result[“REALTIME_PROVEUP_RATE”] < 15)[/php]
or
[php]if ($result[“REALTIME_PROVEUP_RATE”] >= 15)[/php]

ALSO, why all the duplicate code? Just use a variable and use it for the color… Like:
[php]
$color = “FF0000”;
if ($result[“REALTIME_PROVEUP_RATE”] < 15) $color = “339933”;
echo “

” . odbc_result($result, “REALTIME_PROVEUP_RATE”);
[/php]
Or something similar… Less clutter, less IF clause thinking… Simpler, I think… Just my opinion…

[php]if ($result[“REALTIME_PROVEUP_RATE”] < “15”){
echo “

” . odbc_result($result, “REALTIME_PROVEUP_RATE”);
} else {
echo “
” . odbc_result($result, “REALTIME_PROVEUP_RATE”);
}}[/php]

maybe doing something line the following?

[php]$myColor = ($result < 15) ? ‘339933’ : ‘FF0000’;
echo ‘

’ . odbc_result($result, “REALTIME_PROVEUP_RATE”);[/php]

shouldn’t there be a closing

HTML tag and is REALTIME_PROVEUP_RATE a constant, if so no quotes should go around it (but I could be wrong)?

All,

Thank you for the replies and example scripts.

astonecipher - The number is numeric. Real(float). Yes, I have tried it both ways and still get the same result.

ErnieAlex - I tried this also with same results. Same as you, trying to clean the code up a bit.

Strider64 - I tried this morning with no luck. REALTIME_PROVEUP_RATE is a Real (Float) that is queried from a SQL db every minute.

Well, Billy, the problem is the values inside the odbc_result($result, “REALTIME_PROVEUP_RATE”). We all
compared it to what you gave us. The results is using a constant in an array $result, but, that is NOT the
data that is pulled from the database. I am guessing that you just messed up your compare… It’s obvious
to me that most likely you are comparing the wrong value!

So, combining Strider’s two lines and my guess at the error, try this version:
[php]
$myColor = (odbc_result($result, “REALTIME_PROVEUP_RATE”) < 15) ? ‘339933’ : ‘FF0000’;
echo ‘

’ . odbc_result($result, “REALTIME_PROVEUP_RATE”);
[/php]
Astonecipher pointed out that you were using a constant and not a value, but, perhaps it was not worded
clear enough for you to understand.

Hope that solves it…

That got it ErnieAlex. It’s working perfectly. Sorry for the confusion on my part. Yes, I was comparing the wrong value expecting the correct result.

Thank you everyone for you patience and all your help.

Congrats! Always a nice warm fuzzy feeling when you solve a programming puzzle !

I will mark this one solved…

Sponsor our Newsletter | Privacy Policy | Terms of Service