Sample PHP Code: Formula Calculations

<?xml version="1.0" encoding="us-ascii"?>

<?php

  function isBetween($val, $a, $b)

  {  

    if (is_numeric($val))

    {

      return floatval($val) >= $a && floatval($val) <= $b ? floatval($val) : "false";

    }

    else

      return false;

  }

 

  $isPostback=$_POST["btnCalculate"]!=null;

 

  $current_age = isBetween($_POST["lstCurrent_Age"], 0, 65)?$_POST["lstCurrent_Age"]:36;

  $desired_retirement_age = isBetween($_POST["lstDesired_Retirement_Age"], 45, 85)?$_POST["lstDesired_Retirement_Age"]:80;

  $monthly_savings = isBetween($_POST["txtMonthly_Savings"], 1, 100000)?$_POST["txtMonthly_Savings"]:100;

  $annual_investment_growth_rate = isBetween($_POST["txtAnnual_Investment_Growth_Rate"], 0, 1)?$_POST["txtAnnual_Investment_Growth_Rate"]:0.05;

  $annual_salary_increase_rate = isBetween($_POST["txtAnnual_Salary_Increase_Rate"], 0, 1)?$_POST["txtAnnual_Salary_Increase_Rate"]:0.05;

 

  $ret_nest_egg = "";

  $duration = "";

 

  if($isPostback==true)

  {

              $psc = new COM("PagosSpreadSheet.Application")

                        or die("Pagos Spreadsheet component is not installed");

              $psc->Workbooks()->Open(realpath("Spreadsheets/Retirement/Retirement.xls"));

              $workbook = $psc->Workbooks->Item(1);

             

              //Set input values   

              $workbook->Names()->Item("Current_Age")->Range->Value = intval($_POST["lstCurrent_Age"]);

              $workbook->Names()->Item("Desired_Retirement_Age")->Range->Value = intval($_POST["lstDesired_Retirement_Age"]);

              $workbook->Names()->Item("Monthly_savings")->Range->Value = $monthly_savings;

              $workbook->Names()->Item("Annual_Investment_growth_rate")->Range->Value = $annual_investment_growth_rate;

              $workbook->Names()->Item("Annual_Salary_Increase_Rate")->Range->Value = $annual_salary_increase_rate;

                       

              $ret_nest_egg = $workbook->Names()->Item("Retirement_Nest_Egg")->Range->Value;

              $duration     = $workbook->Names()->Item("duration")->Range->Value;

  }

  $ret_nest_egg = "$" . number_format($ret_nest_egg, 2);

 

  function writeError($val)

  {

    echo "<br/><span style=\"color: red\">" . $val . "</span>";

  }

?>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

      <title>Retirement</title>

      <style type="text/css">

            TD, INPUT, SELECT

            {

            font-family: Arial;

            font-size: 10pt;

            }

 

            TABLE#HTInput, #HTOutput

            {

            border-top:solid 1px #C0C0C0;

            border-left:solid 1px #C0C0C0;

            }

 

            TABLE#HTInput TD, TABLE#HTOutput TD

            {

            border-bottom:solid 1px #C0C0C0;

            border-right:solid 1px #C0C0C0;

            }

 

            TABLE#HTOutput TD

            {

            background-color:#CCCCFF;

            }

      </style>

</head>

 

<body>

  <form name="form1" id="form1" action="Retirement.php" method="post">

            <table id="HTInput" border="0" cellpadding="0" cellspacing="0" width="600">

                  <tr>

                        <td style="width: 328px">Current Age:</td>

                        <td><select name="lstCurrent_Age" id="lstCurrent_Age">

          <?php

            for ($i = 0; $i < 66; $i++)

                        if($i==$current_age)

                             echo "<option value='" .$i. "' selected>" . $i . "</option>";

                        else

                             echo "<option value='" .$i. "'>" . $i . "</option>";

          ?>

          </select>

        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px">Desired Retirement Age:</td>

                        <td>

                          <select name="lstDesired_Retirement_Age">

          <?php

            for ($i = 45; $i < 86; $i++)

                        if($i==$desired_retirement_age)

                             echo "<option value='" .$i. "' selected>" . $i . "</option>";

                        else

                             echo "<option value='" .$i. "'>" . $i . "</option>";

          ?> 

          </select>

        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px">Monthly savings:</td>

                        <td>

                          <input name="txtMonthly_Savings" value='<?php echo $monthly_savings ?>' type="text" />

                          <?php

                            if (!$monthly_savings)

                              writeError("Montly savings must be in between 1 and 100000");

                          ?>

                        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px">Annual Investment growth rate (between 0-1):</td>

                        <td>

                          <input name="txtAnnual_Investment_Growth_Rate" value="<?php echo $annual_investment_growth_rate ?>" type="text" />

                        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px">Annual Salary Increase Rate (between 0-1):</td>

                        <td>

                          <input name="txtAnnual_Salary_Increase_Rate" value="<?php echo $annual_salary_increase_rate ?>" type="text" />

                        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px">&nbsp;</td>

                        <td>&nbsp;</td>

                  </tr>

                  <tr>

                        <td style="width: 328px">&nbsp;</td>

                        <td><input name="btnCalculate" type="submit" value="Calculate" /></td>

                  </tr>

            </table>

            <?php

            if($isPostback==true)

            {

            ?>

            <br />

            <table id="HTOutput" border="0" cellpadding="0" cellspacing="0" width="600">

                  <tr>

                        <td style="width: 328px">Retirement Nest Egg in $:</td>

                        <td>

                             <span id="LblRetirement_Nest_Egg" style="font-weight:bold;"><?php echo $ret_nest_egg ?>

                        </span>

                        </td>

                  </tr>

                  <tr>

                        <td style="width: 328px; height: 19px;">Duration (max 100 years):</td>

                        <td style="height: 19px">

                             <span id="Lblduration" style="font-weight:bold;"><?php echo $duration ?>

                        </span>

                        </td>

                  </tr>

            </table>

        <?php

            }

            ?>

  </form>

 

</body>

 

</html>