How and where to code if/then logic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jej1216
    New Member
    • Aug 2006
    • 40

    How and where to code if/then logic

    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.
    [code=php]
    ...
    while ($myrow = mysql_fetch_arr ay($result))
    {
    echo "<tr><td>".$myr ow["facility"]."</td>";
    echo "<td align=right>$". number_format($ myrow["sw_actual"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["sw_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_actual "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_budget "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_actual"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>".$ myrow["pd_actual"]."</td>";
    echo "<td align=right>".$ myrow["pd_budget"]."</td></tr>";
    }
    echo "</tr></table>";
    ...
    [/code]
    I want something like this:
    [code=php]
    If ($myrow["sw_actual"] > $myrow["sw_budget"])
    { fontcolor=red;
    }
    [/code]
    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
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Originally posted by jej1216
    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.
    [code=php]
    ...
    while ($myrow = mysql_fetch_arr ay($result))
    {
    echo "<tr><td>".$myr ow["facility"]."</td>";
    echo "<td align=right>$". number_format($ myrow["sw_actual"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["sw_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_actual "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_budget "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_actual"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>".$ myrow["pd_actual"]."</td>";
    echo "<td align=right>".$ myrow["pd_budget"]."</td></tr>";
    }
    echo "</tr></table>";
    ...
    [/code]
    I want something like this:
    [code=php]
    If ($myrow["sw_actual"] > $myrow["sw_budget"])
    { fontcolor=red;
    }
    [/code]
    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:
    [code=php]
    ...
    while ($myrow = mysql_fetch_arr ay($result))
    {
    If ($myrow["sw_actual"] > $myrow["sw_budget"])
    {
    $style="color:r ed;";
    }

    echo "<tr><td>".$myr ow["facility"]."</td>";
    echo "<td align=right>$". number_format($ myrow["sw_actual"], 0, '.', ',')."</td>";
    echo "<td align=right style='".$style ."'>$".number_f ormat($myrow["sw_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_actual "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["ben_budget "], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_actual"], 0, '.', ',')."</td>";
    echo "<td align=right>$". number_format($ myrow["cl_budget"], 0, '.', ',')."</td>";
    echo "<td align=right>".$ myrow["pd_actual"]."</td>";
    echo "<td align=right>".$ myrow["pd_budget"]."</td></tr>";
    }
    echo "</tr></table>";
    [/code]

    good luck

    Comment

    • jej1216
      New Member
      • Aug 2006
      • 40

      #3
      Thanks - that was it. I had to add an else statement to keep all actuals to show red:
      [code=php]
      while ($myrow = mysql_fetch_arr ay($result))
      {
      if ($myrow["sw_actual"] > $myrow["sw_budget"]) {
      $style1="color: red;"; }
      else {
      $style1="color: black;"; }

      if ($myrow["ben_actual "] > $myrow["ben_budget "]) {
      $style2="color: red;"; }
      else {
      $style2="color: black;"; }

      if ($myrow["cl_actual"] > $myrow["cl_budget"]) {
      $style3="color: red;"; }
      else {
      $style3="color: black;"; }

      if ($myrow["pd_actual"] < $myrow["pd_budget"]) {
      $style4="color: red;"; }
      else {
      $style4="color: black;"; }
      [/code]
      This works, but the code looks clunky - is there a more elegant code to use?

      TIA,

      jej1216

      Comment

      • mwasif
        Recognized Expert Contributor
        • Jul 2006
        • 802

        #4
        You can use Ternary Operator.

        [PHP]while ($myrow = mysql_fetch_arr ay($result))
        {
        $style1 = ($myrow["sw_actual"] > $myrow["sw_budget"]) ? "color:red; " : "color:blac k;";
        $style2 = ($myrow["ben_actual "] > $myrow["ben_budget "]) ? "color:red; " : "color:blac k;";
        $style3 = ($myrow["cl_actual"] > $myrow["cl_budget"]) ? "color:red; " : "color:blac k;";
        $style4 = ($myrow["pd_actual"] < $myrow["pd_budget"]) ? "color:red; " : "color:blac k;";
        }[/PHP]

        Comment

        • jej1216
          New Member
          • Aug 2006
          • 40

          #5
          Originally posted by mwasif
          You can use Ternary Operator.

          [PHP]while ($myrow = mysql_fetch_arr ay($result))
          {
          $style1 = ($myrow["sw_actual"] > $myrow["sw_budget"]) ? "color:red; " : "color:blac k;";
          $style2 = ($myrow["ben_actual "] > $myrow["ben_budget "]) ? "color:red; " : "color:blac k;";
          $style3 = ($myrow["cl_actual"] > $myrow["cl_budget"]) ? "color:red; " : "color:blac k;";
          $style4 = ($myrow["pd_actual"] < $myrow["pd_budget"]) ? "color:red; " : "color:blac k;";
          }[/PHP]
          Thanks - that is pretty slick code.

          Comment

          Working...