Page 1 of 1

Interest rate calculus better: HOW?(please review this code)

Posted: Sat Jun 21, 2008 10:06 pm
by Rosamunda
Hi there!
I´m starting to study PHP, and I´ve come up with a bit of code that shows the interest that a person has to pay for a certain aumont of money. My mother´s tongue isn´t english (but spanish), please forgive me for my bad grammar!

Let´s say that a person haven´t pay the monthly expenses of the maintenance of its building (don´t know how it´s called in english) for a couple of months now, according to this table:
In march/2008 he should have been paying $300.
In april/2008 he should have been paying $250.
In may/2008 he should have been paying $200.
In june/2008 he should have been paying $350.
They are variable, and we cannot add the interes to the capital, so we have to calculate capital and interest in two different columns.

I´ve come up with this, that actually works:
THIS IS THE FORM THAT THE USER SHOULD COMPLETE IN ORDER TO DO THE CALCULATION:

Code: Select all

<html>
<form action="calcular_deuda.php" method="post">
<b>Annual Interest Rate: <input type ="text" name="interes_anual" size="5">%</b><br>
<br>
<table>
<tr><td>Month indebted:<br>
<input type ="text" name="f1" size="15"></td>
<td>How much?:<br>
<input type ="text" name="m1" size="5"></td></tr>
 
<tr><td>Month indebted:<br>
<input type ="text" name="f2" size="15"></td>
<td>How much?:<br>
<input type ="text" name="m2" size="5"></td></tr>
 
<tr><td>Month indebted:<br>
<input type ="text" name="f3" size="15"></td>
<td>How much?:<br>
<input type ="text" name="m3" size="5"></td></tr>
 
<tr><td>Month indebted:<br>
<input type ="text" name="f4" size="15"></td>
<td>How much?:<br>
<input type ="text" name="m4" size="5"></td></tr>
</table>
 
<br>
<input type="submit" name="submit" value="Calculate" size="5">
 
</form>
</html>
Now that´s the form in html.
The php code is this:

Code: Select all

 
 
$var = 1200;
 
$date1 = date("Y/n/d");
$activationdate1 = date("Y/n/d", strtotime ($HTTP_POST_VARS['f1']));
$years1= date("Y", strtotime("now")) - date("Y", strtotime($activationdate1));
 
 
if (date ("Y", strtotime($date1)) == date ("Y", strtotime($activationdate1))){
$months1 = date ("m", strtotime($date1)) - date ("m", strtotime($activationdate1));
}
elseif ($years1 == "1"){
$months1 = (date ("m", strtotime("December")) - date ("m", strtotime($activationdate1))) + (date ("m"));
}
elseif($years1 >= "2"){
$months1 = ($years1*12) + (date ("m", strtotime("now")) - date ("m", strtotime($activationdate1)));
}
 
$interesf1= $HTTP_POST_VARS["m1"] * $months1 * $HTTP_POST_VARS["interes_anual"] / $var;
echo "El Interés del período ".$HTTP_POST_VARS["m1"]." es de ".$interesf1."<br>";
 
 
$date2 = date("Y/n/d");
$activationdate2 = date("Y/n/d", strtotime ($HTTP_POST_VARS['f2']));
$years2= date("Y", strtotime("now")) - date("Y", strtotime($activationdate2));
 
if (date ("Y", strtotime($date2)) == date ("Y", strtotime($activationdate2))){
$months2 = date ("m", strtotime($date2)) - date ("m", strtotime($activationdate2));
}
elseif ($years2 == "1"){
$months2 = (date ("m", strtotime("December")) - date ("m", strtotime($activationdate2))) + (date ("m"));
}
elseif($years2 >= "2"){
$months2 = ($years*12) + (date ("m", strtotime("now")) - date ("m", strtotime($activationdate2)));
}
 // echo 'Pasaron '.$months.' meses.';
 
$interesf2= $HTTP_POST_VARS["m2"] * $months2 * $HTTP_POST_VARS["interes_anual"] / $var;
echo "El Interés del período ".$HTTP_POST_VARS["m2"]." es de ".$interesf2."<br>";
 
 
$date3 = date("Y/n/d");
$activationdate3 = date("Y/n/d", strtotime ($HTTP_POST_VARS['f3']));
$years3= date("Y", strtotime("now")) - date("Y", strtotime($activationdate3));
 
if (date ("Y", strtotime($date3)) == date ("Y", strtotime($activationdate3))){
$months3 = date ("m", strtotime($date3)) - date ("m", strtotime($activationdate3));
}
elseif ($years3 == "1"){
$months3 = (date ("m", strtotime("December")) - date ("m", strtotime($activationdate3))) + (date ("m"));
}
elseif($years3 >= "2"){
$months3 = ($years3*12) + (date ("m", strtotime("now")) - date ("m", strtotime($activationdate3)));
}
 
$interesf3= $HTTP_POST_VARS["m3"] * $months3 * $HTTP_POST_VARS["interes_anual"] / $var;
echo "El Interés del período ".$HTTP_POST_VARS["m3"]." es de ".$interesf3."<br>";
 
 
$date4 = date("Y/n/d");
$activationdate4 = date("Y/n/d", strtotime ($HTTP_POST_VARS['f4']));
$years4= date("Y", strtotime("now")) - date("Y", strtotime($activationdate4));
 
 
if (date ("Y", strtotime($date4)) == date ("Y", strtotime($activationdate4))){
$months4 = date ("m", strtotime($date4)) - date ("m", strtotime($activationdate4));
}
elseif ($years4 == "1"){
$months4 = (date ("m", strtotime("December")) - date ("m", strtotime($activationdate4))) + (date ("m"));
}
elseif($years4 >= "2"){
$months4 = ($years4*12) + (date ("m", strtotime("now")) - date ("m", strtotime($activationdate4)));
}
 
$interesf4= $HTTP_POST_VARS["m4"] * $months4 * $HTTP_POST_VARS["interes_anual"] / $var;
echo "El Interés del período ".$HTTP_POST_VARS["m4"]." es de ".$interesf4."<br>";
 
$totalcapital= $HTTP_POST_VARS["m1"] + $HTTP_POST_VARS["m2"] + $HTTP_POST_VARS["m3"] + $HTTP_POST_VARS["m4"];
echo "Total de Capital Adeudado: ".$totalcapital."<br>";
 
$totalintereses= $interesf1 + $interesf2 + $interesf3 + $interesf4;
echo "Total Intereses: ".$totalintereses."<br>";
 
$total_final= $totalcapital + $totalintereses;
echo "Total Final: ".$total_final;
 
Now, this actually works, but I´m having a big fat problem here:
I should add a long piece of code for each month added!
And what if the person that does the form input (and naturally does not have access to the form´s code) want to add more slots because there are more months to calculate?


THANKS FOR YOUR HELP!!!!!!!!!!!!!!

Re: Interest rate calculus better: HOW?(please review this code)

Posted: Sun Jun 22, 2008 4:15 pm
by Rosamunda
Any ideas?

Re: Interest rate calculus better: HOW?(please review this code)

Posted: Sun Jun 22, 2008 7:31 pm
by Kieran Huggins
You can add some JS that generates a new form row (harder, nicer), or you can just provide a lot of them (easier).

Then you can perform your calculations in a loop, like so:

Code: Select all

<input type="text" name="month[]"/>
<input type="text" name="rent[]"/>
 
<input type="text" name="month[]"/>
<input type="text" name="rent[]"/>
 
<input type="text" name="month[]"/>
<input type="text" name="rent[]"/>

Code: Select all

while( count($_POST['month']) > 0 && count($_POST['rent']) > 0  ){
  $month = array_shift($_POST['month']);
  $rent = array_shift($_POST['rent']);
  // calculate interest on the rent for this month, add it to the total
}