PHP Mortgage Calculator - TechRepublic
General discussion
April 14, 2004 at 09:26 AM
sophiahope

PHP Mortgage Calculator

by sophiahope . Updated 22 years, 2 months ago

PHP Mortgage Calculator
I have stitched together the beginnings of a mortgage calculator, but need to add features which are beyond me right now. Current: http://s100053443.onlinehome.us/loan-calculator.php
Need to:
1) make additional principle payment field calculate into the graph and the summary table showing difference between the extra paymment and no extra payment.
2) want to make the amortization table yearly instead of per period.
Can you help???
Thanks!
Graph code:

Calculator code:
‘Weekly’,
26 => ‘Bi-weekly’,
12 => ‘Monthly’,
6 => ‘Bi-monthly’,
4 => ‘Quarterly’,
2 => ‘Semi-annually’,
1 => ‘Annually’
);
$loan_amount = isset($_GET[“loan_amount”]) ? $_GET[“loan_amount”] : 150000;
$loan_length = isset($_GET[“loan_length”]) ? $_GET[“loan_length”] : 30;
$annual_interest = isset($_GET[“annual_interest”]) ? $_GET[“annual_interest”] : 4.75;
$pay_periodicity = isset($_GET[“pay_periodicity”]) ? $_GET[“pay_periodicity”] : 12;
$periodicity = $periods[$pay_periodicity];

// Additional Monthly Principle Payment
$add_prin_pmt = isset($_GET[“add_prin_pmt”]) ? $_GET[“add_prin_pmt”] : 0;

$pay_periods = ”;
foreach($periods as $value => $name)
{
$selected = ($pay_periodicity == $value) ? ‘selected’ : ”;
$pay_periods .= replace_vars($pay_period_option);
}

/* Checking Variables */
if(!is_numeric($loan_amount) or $loan_amount <= 0) { $error = "

Loan amount has to be numeric and greater than zero.

“; }

if(!is_numeric($loan_length) or $loan_length <= 0) { $error = "

Loan length has to be numeric and greater than zero.

“; }

if(!is_numeric($annual_interest) or $annual_interest <= 0) { $error = "

Annual interest has to be numeric and greater than zero.

“; }

/*
*———————————-
* Calculating and displaying
*———————————-
*/

/* Process Loan Parameters Form in any case */
$loan_parameters_form = replace_vars($loan_parameters_form);

/* If no error, then begin calculations */
if(!isset($error) && isset($_GET[‘action’]))
{
$c_balance = $loan_amount;
$total_periods = $loan_length * $pay_periodicity;
$interest_percent = $annual_interest / 100;
$period_interest = $interest_percent / $pay_periodicity;
$c_period_payment = $loan_amount * ($period_interest / (1 – pow((1 + $period_interest), -($total_periods))));
$total_paid = number_format($c_period_payment * $total_periods, 2, ‘.’, ‘,’);
$total_interest = number_format($c_period_payment * $total_periods – $loan_amount, 2, ‘.’, ‘,’);
$total_principal = number_format($loan_amount, 2, ‘.’, ‘,’);

$loan_amount = number_format($loan_amount, 2, ‘.’, ‘,’);
$annual_interest = number_format($annual_interest, 2, ‘.’, ‘,’);
$period_payment = number_format($c_period_payment, 2, ‘.’, ‘,’);

$amortization_table_rows = ”;
for($period = 1; $period <= $total_periods; $period++) { $c_interest = $c_balance * $period_interest; $c_principal = $c_period_payment - $c_interest; $c_balance -= $c_principal; $interest = number_format($c_interest, 2, '.', ','); $principal = number_format($c_principal, 2, '.', ','); $balance = number_format($c_balance, 2, '.', ','); $evenrow_row_modifier = ($period % 2) ? '' : 'class=evenrow'; $amortization_table_rows .= replace_vars($amortization_table_row); } } else { $amortization_table = ''; $loan_summary = ''; } $body = replace_vars($body); /* If headers sent, then it means that the script is used as inclusion */ if(!headers_sent()) { $send_footer = true; echo strip($header); } echo strip(replace_vars($body)); if(isset($send_footer)) { echo strip($footer); } /* *---------------------------------- * Functions *---------------------------------- */ function replace_vars($tpl) { return preg_replace_callback("/\{(.+?)\}/", "glb", $tpl); } function glb($m) { if(isset($GLOBALS[$m[1]])) { return $GLOBALS[$m[1]]; } } function load_template($path) { /* Loads template from file */ $h = fopen($path, "r"); $file = fread($h, filesize($path)); fclose($h); preg_match_all('/(.+?)/is’, $file, $m);

for($i=0; $i

This discussion is locked

All Comments