473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How and where to code if/then logic

40
I have a PHP page that displays in a table budget vs. actual figures from a MySQL db. I want to add logic where if the actual is greater than the budget, the data is displayed in red, otherwise display in black.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. while ($myrow = mysql_fetch_array($result))
  3. {
  4. echo "<tr><td>".$myrow["facility"]."</td>";
  5. echo "<td align=right>$".number_format($myrow["sw_actual"], 0, '.', ',')."</td>";
  6. echo "<td align=right>$".number_format($myrow["sw_budget"], 0, '.', ',')."</td>";
  7. echo "<td align=right>$".number_format($myrow["ben_actual"], 0, '.', ',')."</td>";
  8. echo "<td align=right>$".number_format($myrow["ben_budget"], 0, '.', ',')."</td>";
  9. echo "<td align=right>$".number_format($myrow["cl_actual"], 0, '.', ',')."</td>";
  10. echo "<td align=right>$".number_format($myrow["cl_budget"], 0, '.', ',')."</td>";
  11. echo "<td align=right>".$myrow["pd_actual"]."</td>";
  12. echo "<td align=right>".$myrow["pd_budget"]."</td></tr>";
  13. }
  14. echo "</tr></table>";
  15. ...
  16.  
I want something like this:
Expand|Select|Wrap|Line Numbers
  1. If ($myrow["sw_actual"] > $myrow["sw_budget"])
  2. { fontcolor=red;
  3. }
  4.  
I tried this prior to the While code, and got an error with the 'fontcolor=red' line. Parse error: parse error, unexpected '=' in /usr/local/php_classes/swb_2007_01.php on line 88.
I tried 'fontcolor: red' as well as 'fontcolor red', but get a parse error for each.

Is my If statement incorrect? Should it be placed elswhere?

TIA,

jej1216
Jul 6 '07 #1
4 1560
epots9
1,351 Expert 1GB
I have a PHP page that displays in a table budget vs. actual figures from a MySQL db. I want to add logic where if the actual is greater than the budget, the data is displayed in red, otherwise display in black.
Expand|Select|Wrap|Line Numbers
  1. ...
  2. while ($myrow = mysql_fetch_array($result))
  3. {
  4. echo "<tr><td>".$myrow["facility"]."</td>";
  5. echo "<td align=right>$".number_format($myrow["sw_actual"], 0, '.', ',')."</td>";
  6. echo "<td align=right>$".number_format($myrow["sw_budget"], 0, '.', ',')."</td>";
  7. echo "<td align=right>$".number_format($myrow["ben_actual"], 0, '.', ',')."</td>";
  8. echo "<td align=right>$".number_format($myrow["ben_budget"], 0, '.', ',')."</td>";
  9. echo "<td align=right>$".number_format($myrow["cl_actual"], 0, '.', ',')."</td>";
  10. echo "<td align=right>$".number_format($myrow["cl_budget"], 0, '.', ',')."</td>";
  11. echo "<td align=right>".$myrow["pd_actual"]."</td>";
  12. echo "<td align=right>".$myrow["pd_budget"]."</td></tr>";
  13. }
  14. echo "</tr></table>";
  15. ...
  16.  
I want something like this:
Expand|Select|Wrap|Line Numbers
  1. If ($myrow["sw_actual"] > $myrow["sw_budget"])
  2. { fontcolor=red;
  3. }
  4.  
I tried this prior to the While code, and got an error with the 'fontcolor=red' line. Parse error: parse error, unexpected '=' in /usr/local/php_classes/swb_2007_01.php on line 88.
I tried 'fontcolor: red' as well as 'fontcolor red', but get a parse error for each.

Is my If statement incorrect? Should it be placed elswhere?

TIA,

jej1216
try this:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. while ($myrow = mysql_fetch_array($result))
  3. {
  4. If ($myrow["sw_actual"] > $myrow["sw_budget"])
  5. {
  6. $style="color:red;";
  7. }
  8.  
  9. echo "<tr><td>".$myrow["facility"]."</td>";
  10. echo "<td align=right>$".number_format($myrow["sw_actual"], 0, '.', ',')."</td>";
  11. echo "<td align=right style='".$style."'>$".number_format($myrow["sw_budget"], 0, '.', ',')."</td>";
  12. echo "<td align=right>$".number_format($myrow["ben_actual"], 0, '.', ',')."</td>";
  13. echo "<td align=right>$".number_format($myrow["ben_budget"], 0, '.', ',')."</td>";
  14. echo "<td align=right>$".number_format($myrow["cl_actual"], 0, '.', ',')."</td>";
  15. echo "<td align=right>$".number_format($myrow["cl_budget"], 0, '.', ',')."</td>";
  16. echo "<td align=right>".$myrow["pd_actual"]."</td>";
  17. echo "<td align=right>".$myrow["pd_budget"]."</td></tr>";
  18. }
  19. echo "</tr></table>";
  20.  
good luck
Jul 6 '07 #2
jej1216
40
Thanks - that was it. I had to add an else statement to keep all actuals to show red:
Expand|Select|Wrap|Line Numbers
  1.     while ($myrow = mysql_fetch_array($result))
  2.     {
  3.      if ($myrow["sw_actual"] > $myrow["sw_budget"])    {
  4.      $style1="color:red;";    }
  5.      else    {
  6.      $style1="color:black;";    }
  7.  
  8.      if ($myrow["ben_actual"] > $myrow["ben_budget"])    {
  9.      $style2="color:red;";    }
  10.      else    {
  11.      $style2="color:black;";    }
  12.  
  13.      if ($myrow["cl_actual"] > $myrow["cl_budget"])    {
  14.      $style3="color:red;";    }
  15.      else    {
  16.      $style3="color:black;";    }
  17.  
  18.      if ($myrow["pd_actual"] < $myrow["pd_budget"])    {
  19.      $style4="color:red;";    }
  20.      else    {
  21.      $style4="color:black;";    }
  22.  
This works, but the code looks clunky - is there a more elegant code to use?

TIA,

jej1216
Jul 6 '07 #3
mwasif
802 Expert 512MB
You can use Ternary Operator.

[PHP]while ($myrow = mysql_fetch_array($result))
{
$style1 = ($myrow["sw_actual"] > $myrow["sw_budget"]) ? "color:red;" : "color:black;";
$style2 = ($myrow["ben_actual"] > $myrow["ben_budget"]) ? "color:red;" : "color:black;";
$style3 = ($myrow["cl_actual"] > $myrow["cl_budget"]) ? "color:red;" : "color:black;";
$style4 = ($myrow["pd_actual"] < $myrow["pd_budget"]) ? "color:red;" : "color:black;";
}[/PHP]
Jul 6 '07 #4
jej1216
40
You can use Ternary Operator.

[PHP]while ($myrow = mysql_fetch_array($result))
{
$style1 = ($myrow["sw_actual"] > $myrow["sw_budget"]) ? "color:red;" : "color:black;";
$style2 = ($myrow["ben_actual"] > $myrow["ben_budget"]) ? "color:red;" : "color:black;";
$style3 = ($myrow["cl_actual"] > $myrow["cl_budget"]) ? "color:red;" : "color:black;";
$style4 = ($myrow["pd_actual"] < $myrow["pd_budget"]) ? "color:red;" : "color:black;";
}[/PHP]
Thanks - that is pretty slick code.
Jul 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Simon Harvey | last post by:
Hello Chaps, Me and a collegue have been talking about where the best place to put business logic is. I think that the best place is where Microsoft suggest - in a seperate business logic...
8
by: jvyyuie | last post by:
// My email is jvyyuie@hotmail.com // I want to make some friends and discuss about programing. #include "stdafx.h" #include <malloc.h> #include <string.h> #define sec1...
9
by: Steve Jorgensen | last post by:
First, an example - today, I wanted to print out the parameter values in a querydef object at a break point in the code for subsequent manual debugging of the query. In the past, I would have had...
7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
1
by: Chris Kettenbach | last post by:
Hi I am designing an ASP.net app. Just wanted some opinions on code behind options. My idea is to have a code behind file that the controls will reference and have ascx files for the controls. ...
21
by: Brett | last post by:
Say I develop an application that has an SQL Server 2000 back end with ASP.NET front end. All the business logic is in the aspx pages. I want to sell this as a package that some one can install...
2
by: Phillip N Rounds | last post by:
I'm working on a UserControl ( ASP.NET 1.1, C#, VS 2003), where I can't manage the state of multiple controls correctly. The controls have the following constraints 1: It has two possible...
2
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
21
by: stephenrussett | last post by:
Ok i what i am trying to do is: I have a query that pumps out information like this: (1) (2) (3) (4) (5) 1 1 Cooper Street NS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.