Re: [PHP] Division by 0

[email protected] (Joseph Thayne)
Newsgroups php.general
Message-ID <[email protected]>
Looks to me like you are closing your form before you put anything in 
it.  Therefore, the loan_amount is not set making the value 0.  Follow 
the math, and you are dividing by 1-1.

Change this line:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"></form>

to:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

and you should be good to go.

Joseph

Gary wrote:
> I have a mortgage amortization script that was working fine,now it seems to 
> have gone awry. Below is the entire script plus input page.  I am getting an 
> error
>
> Warning: Division by zero in 
> /home/content/J/a/y/Jayski/html/one2one/Ricksrecursivefunctions.php on line 
> 47
>
> Which is  (pow($intCalc,$totalPayments) - 1);
>
> Frankly I am not even sure the information is being passed to the script.
>
> Anyone see what I am missing?
>
> Gary
>
>
> <div id="onecol">Calculate your Loan</div>
> <div id="leftcontent">
>
> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"></form>
> <table>
>  <tr>
>       <td style="background-color:#B1D8D8" width="110px">Loan Amount</td>
>         <td><input name="loan_amount" type="text" size="25" /> USD</td>
>         <td><a href="javascript:void(0);"  onmouseover="Tip('This is the 
> amount of money to be loaned.')" onmouseout="UnTip()"><img 
> src="images/help.png" class="noborder"/></a></td>
>   </tr>
> <tr>
>       <td style="background-color:#B1D8D8" width="110px">Type of Loan</td>
>       <td>
> <select name="type" size="1" id="type">
>   <option>Installment</option>
>   <option>Balloon</option>
> </select></td>
> <td><a href="javascript:void(0);"  onmouseover="Tip('This is the method of 
> repayment.')" onmouseout="UnTip()"><img src="images/help.png" 
> class="noborder"/></a></td>
>   </tr>
>    <tr>
>      <td style="background-color:#B1D8D8" width="100px">Term of Loan</td>
>         <td><input name="loan_term" type="text" size="5" />
> </select>Months</td>
> <td><a href="javascript:void(0);"  onmouseover="Tip('This is the amount of 
> time that the money is loaned for.')" onmouseout="UnTip()"><img 
> src="images/help.png" class="noborder" /></a></td>
>         </tr>
>      <tr>
>      <td style="background-color:#B1D8D8" width="140px">Interest Rate</td>
>         <td><input name="int_rate" type="text" size="10" /> Per 
> Annum</td><td><a href="javascript:void(0);"  onmouseover="Tip('Percentage 
> (%) charged on loan on an annual basis. <br />Please see our FAQs for 
> information on usury rates. <br />If no amount is entered this will be 
> 0%.')" onmouseout="UnTip()"><img src="images/help.png" class="noborder" 
> /></a></td>
>         </tr>
> </table>
> <label>
> <input type="submit" name="submit" id="submit" value="submit" />
>     </label>
> </form>
> <?php
>
> function amortizationTable($paymentNum, $periodicPayment, $balance,
>                            $monthlyInterest) {
>     $paymentInterest = round($balance * $monthlyInterest,2);
>     $paymentPrincipal = round($periodicPayment - $paymentInterest,2);
>     $newBalance = round($balance - $paymentPrincipal,2);
>     print "<tr>
>            <td>$paymentNum</td>
>            <td>\$".number_format($balance,2)."</td>
>            <td>\$".number_format($periodicPayment,2)."</td>
>            <td>\$".number_format($paymentInterest,2)."</td>
>            <td>\$".number_format($paymentPrincipal,2)."</td>
>            </tr>";
>      # If balance not yet zero, recursively call amortizationTable()
>      if ($newBalance > 0) {
>         $paymentNum++;
>         amortizationTable($paymentNum, $periodicPayment, $newBalance,
>                           $monthlyInterest);
>      } else {
>         exit;
>      }
> } #end amortizationTable()
>
>    # Loan balance
>    $balance =($_POST['loan_amount']);
>
>    # Loan interest rate
>    $interestRate = ($_POST['int_rate']);
>
>    # Monthly interest rate
>    $monthlyInterest = ("$interestRate / 12");
>
>    # Term length of the loan, in years.
>    $termLength =($_POST['loan_term']);
>
>    # Number of payments per year.
>    $paymentsPerYear = 12;
>
>    # Payment iteration
>    $paymentNumber =($_POST['loan_term']);
>
>    # Perform preliminary calculations
>    $totalPayments = $termLength * $paymentsPerYear;
>    $intCalc = 1 + $interestRate / $paymentsPerYear;
>    $periodicPayment = $balance * pow($intCalc,$totalPayments) * ($intCalc - 
> 1) /
>                                     (pow($intCalc,$totalPayments) - 1);
>    $periodicPayment = round($periodicPayment,2);
>
>    # Create table
>    echo "<table width='50%' align='center' border='1'>";
>    print "<tr>
>           <th>Payment
>     Number</th><th>Balance</th>
>           <th>Payment</th><th>Interest</th><th>Principal</th>
>           </tr>";
>
>    # Call recursive function
>    amortizationTable($paymentNumber, $periodicPayment, $balance, 
> $monthlyInterest);
>
>    # Close table
>    print "</table>";
>
> ?>
> </div> 
>
>
>
> __________ Information from ESET Smart Security, version of virus signature database 4932 (20100310) __________
>
> The message was checked by ESET Smart Security.
>
> http://www.eset.com
>
>
>
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.