PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Adeola Odusola   PHP Mortgage Calculator   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example
Class: PHP Mortgage Calculator
Calculate monthly payments for mortgage
Author: By
Last change: added html display for mortgage summary and amortization table
Date: 4 years ago
Size: 4,892 bytes
 

Contents

Class file image Download
<?php
/**
 * @author Adeola Odusola
 *
 * The following program calculates monthly mortgage for fixed interest rate.
 * The user inputs the original loan amount, interest rate, and number of years.
 */
?>

<!DOCTYPE html>
<html>
    <head>
        <style>
             table.my_table {
                font-family: verdana,arial,sans-serif;
                color:#333333;
                border-width: 1px;
                border-color: #999999;
                border-collapse: collapse;
            }
            table.my_table th {
                font-size:10px;
                background:#b5cfd2;
                border-width: 1px;
                padding: 3px;
                border-style: solid;
                border-color: #999999;
            }
            table.my_table tr{
                 -webkit-print-color-adjust: exact;
            }
            table.my_table tr:nth-child(odd) {
                background: #f2f2f2;
            }
            table.my_table tr:hover {
                background-color: #ffff00;
            }
            table.my_table td {
                font-size:10px;
                border-width: 1px;
                padding: 2px;
                border-style: solid;
                border-color: #999999;
            }
        </style>
        <title>
            Mortgage calculator and amortization table generator
        </title>
    </head>
    <body>
        <form method="post" action="">
            <table>
                <tr>
                    <td>
                        <label for="loan">Loan: $</label>
                    </td>
                    <td>
                        <input type="text" name="loan" value="" pattern="\d+((\.)\d{2})?" required>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="rate">Rate: </label>
                    </td>
                    <td>
                        <input type="text" name="rate" value="" pattern="\d+((\.)\d{1,})?" required>
                    </td>
                </tr>
                <tr>
                    <td>
                        <label for="years">Years: </label>
                    </td>
                    <td>
                        <select name="years" required>
                            <option value="">select...</option>
                            <option value="5">5</option>
                            <option value="10">10</option>
                            <option value="15">15</option>
                            <option value="20">20</option>
                            <option value="25">25</option>
                            <option value="30">30</option>
                            <option value="40">40</option>
                            <option value="50">50</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                       <input type="submit" name="compute" value="calculate mortgage">
                    </td>
                </tr>
            </table>
        </form>


<?php
if (isset($_POST['compute'])){
    require_once
'Mortgage.php';
   
//input values
   
$loanAmount=$_POST['loan'];
   
$rawRate=$_POST['rate'];
   
$numberOfYears=$_POST['years'];
   
   
// get results
   
$mort=new Mortgage($loanAmount, $rawRate, $numberOfYears);
   
$results=$mort->getResults();
   
$summary=$results["summary"];
   
$amortization=$results["amortization"];
   
   
// display mortgage summary
   
echo "<div>Mortgage Summary</div>";
    echo
"<table class=\"my_table\">";
    echo
"<tr><td>Home Value: </td><td>$".$summary["homeValue"]."</td></tr>";
    echo
"<tr><td>Monthly Mortgage: </td><td>$".$summary["monthlyMortgage"]."</td></tr>";
    echo
"<tr><td>Total Payment: </td><td>$".$summary["totalPayment"]."</td></tr>";
    echo
"<tr><td>Total Interest: </td><td>$".$summary["totalInterest"]."</td></tr>";
    echo
"</table><br>";
       
   
// display amortization table
   
echo "<div>Amortization Schedule</div>";
    echo
"<table class=\"my_table\">";
    echo
"<tr><th>Month #</th><th>Mortgage</th><th>Principal</th><th>Interest</th><th>Balance</th></tr>";
    foreach (
$amortization as $row){
        echo
"<tr>";
        echo
"<td>".$row["monthNumber"]."</td>";
        echo
"<td>".$row["mortgage"]."</td>";
        echo
"<td>".$row["principal"]."</td>";
        echo
"<td>".$row["interest"]."</td>";
        echo
"<td>".$row["balance"]."</td>";
        echo
"</tr>";
        if (
$row["monthNumber"]%12==0 && $row["monthNumber"]!=($numberOfYears*12) ){
           
// redisplay header
           
echo "<tr><th>Month #</th><th>Mortgage</th><th>Principal</th><th>Interest</th><th>Balance</th></tr>";
        }
    }
    echo
"</table>";
}
?>

       
    </body>
</html>