Web Development

General discussion

Locked

PHP Mortgage Calculator

By ·
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:
<?php

// Add values to the graph

// Red Line, with add'l principle pmt
\$graphValues2=array(100,98,97,96,95,93,**,89,87,84,81,78,75,72,66,60,51,40,30,15,0,0,0,0,0,0,0,0,0,0,0,0,0);

// Green Line, no add'l principle pmt
\$graphValues=array(100,99,98,97,96,95,93,**,89,87,85,83,81,79,76,73,70,67,64,61,57,52,49,44,40,35,30,25,20,12,0,0);

// Define .PNG image
\$imgWidth=300;
\$imgHeight=100;

// Create image and define colors
\$image=imagecreate(\$imgWidth, \$imgHeight);
\$colorWhite=imagecolorallocate(\$image, 255, 255, 255);
\$colorLtGrey=imagecolorallocate(\$image, 200, 200, 200);
\$colorGrey=imagecolorallocate(\$image, 192, 192, 192);
\$colorGreen=imagecolorallocate(\$image, 0, 200, 0);
\$colorLtGreen=imagecolorallocate(\$image, 135, 239, 140);
\$colorRed=imagecolorallocate(\$image, 203, 0, 6);

// Create border around image
imageline(\$image, 0, 0, 0, 100, \$colorGrey);
imageline(\$image, 0, 0, 300, 0, \$colorGrey);
imageline(\$image, 299, 0, 299, 99, \$colorGrey);
imageline(\$image, 0, 99, 299, 99, \$colorGrey);

// Create grid
for (\$i=1; \$i<31; \$i++){
imageline(\$image, \$i*10, 0, \$i*10, 100, \$colorLtGrey); // vertical lines
imageline(\$image, 0, \$i*10, 300, \$i*10, \$colorLtGrey); // horz lines
}

for (\$i=0; \$i<30; \$i++){
imageline(\$image, \$i*10, (100-\$graphValues2[\$i]), (\$i+1)*10, (100-\$graphValues2[\$i+1]), \$colorRed);
}

// Green Line (no add'l principle)
for (\$i=0; \$i<30; \$i++){
imageline(\$image, \$i*10, (100-\$graphValues[\$i]), (\$i+1)*10, (100-\$graphValues[\$i+1]), \$colorGreen);
}

// Output graph and clear image from memory
imagepng(\$image);
imagedestroy(\$image);

?>

Calculator code:
<?php

/**
* Project: phpLoanCalc v1.0
* Files: loan-calculator.php
* loan-calculator.htm

/* Defining Variables */
\$periods = array(
52 => '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];

\$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 = "<p><font color=red>Loan amount</font> has to be numeric and greater than zero.</p>"; }

if(!is_numeric(\$loan_length) or \$loan_length <= 0)
{ \$error = "<p><font color=red>Loan length</font> has to be numeric and greater than zero.</p>"; }

if(!is_numeric(\$annual_interest) or \$annual_interest <= 0)
{ \$error = "<p><font color=red>Annual interest</font> has to be numeric and greater than zero.</p>"; }

/*
*----------------------------------
* 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 */
{
\$send_footer = true;
}

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]];
}
}

{
/* Loads template from file */
\$h = fopen(\$path, "r");
fclose(\$h);

preg_match_all('/<!-- BEGIN (.+?) --.+?)<!-- END \1 -->/is', \$file, \$m);

for(\$i=0; \$i<count(\$m[1]); \$i++)
{
\$template[\$m[1][\$i]] = \$m[2][\$i];
}

return \$template;
}

function strip(\$text)
{
/* Removes New Line characters and multiple spaces */
return preg_replace('/\s+/', ' ', \$text);
}
?>

This conversation is currently closed to new comments.

0 total posts (Page 1 of 1)
Thread display: Collapse - | Expand +

Back to Web Development Forum
0 total posts (Page 1 of 1)

Related Discussions

• 15

• 9

• 0

Dave19751 ·

• 18

What is best CMS according you in 2019?

contact1550492909 ·

• 8

What is Importance of Website Design and Web Development

sumanbeltech ·