简体   繁体   中英

Display a variable based on text input and selections in php

I'm currently developing a car loan calculator in PHP. The user inputs are: VEHICLE PURCHASE PRICE, DEPOSIT, INTEREST RATE, BALLOON PERCENTAGE, TERM OF LOAN (in months) and two compulsory fields.

My math works 100% and if you run my script you'll bare witness to that as I've echoed all the answers. The math is not the problem.

More often than not, a user will not complete all the fields. Here are the conditions:

  1. User completes all fields ( $monthlyInstallmentBoth )
  2. User completes PURCHASE PRICE, DEPOSIT, INTEREST and TERM (Didn't select balloon) ( $monthlyInstallmentDeposit )
  3. User completes PURCHASE PRICE, INTEREST, BALLOON and TERM (Didn't select deposit) ( $monthlyInstallmentBalloon )
  4. User completes PURCHASE PRICE, INTEREST and TERM (Didn't select deposit or balloon) ( $mp )

Assume the variables with the above conditions:

  1. $monthlyInstallmentBoth
  2. $monthlyInstallmentDeposit
  3. $monthlyInstallmentBalloon
  4. $mp

Here's my question: How do I display only $monthlyInstallmentBoth when all the options are selected or display only $monthlyInstallmentDeposit when a balloon percentage weren't selected or display only $monthlyInstallmentBalloon if a deposit weren't selected or display only $mp if no deposit and no balloon percentage were selected?

I tried a switch statement but I'm not sure that is what I need at the moment. Because it's not working.

Please see my code below:

<?php
//////////////////
//Math Variables//
//////////////////

// $r = interest
// $p = principle purchase price
// $br = balloon rate in %
// $d = deposit

//balloon percentage in decimals: $br / 100
//balloon amount: $ba = $p x $br
//principle less deposit: $dp = $p - $d

// $x = formula to calculate amount for p to be devided by

//monthly installment: $mp = $np / $Sx

////////////////////////
//Variables from input//
////////////////////////

//$principle (textbox) [name=principle]
//$deposit (textbox) [name=deposit]
//$term (dropdown) [name=term]
//$interest (dropdown) [name=interest]
//$balloon (dropdown) [name=ballon]
//57 (disabled input) = 57 (monthly) [name=admin]
//$initiation (disabled input) = 1140 [name=initiation]

?>

<form method="post" action="">

<label for="principle">What is the total purchase price?</label>
<input type="text" name="principle" id="principle" value="100000">

<label for="deposit">How much deposit are you paying?</label>
<input type="text" name="deposit" id="deposit" value="0">

<label for="term">How many months to repay the loan?</label>
<select name="term" id="term">
    <option>72</option>
    <option>60</option>
    <option>54</option>
    <option>48</option>
    <option>36</option>
    <option>24</option>
    <option>12</option>
</select>

<label for="balloon">What balloon % would you like, if any?</label>
<select name="balloon" id="balloon">
    <option>0</option>
    <option>5</option>
    <option>10</option>
    <option>15</option>
    <option>20</option>
    <option>25</option>
    <option>30</option>
    <option>35</option>
    <option>40</option>
    <option>45</option>
    <option>50</option>
</select>

<label for="interest">What interest rate will you be paying?</label>
<select name="interest" id="interest">
    <option>7</option>
    <option>7.5</option>
    <option>8</option>
    <option>8.5</option>
    <option>9</option>
    <option>9.5</option>
    <option>10</option>
    <option>10.5</option>
    <option>11</option>
    <option>11.5</option>
    <option>12</option>
    <option>12.5</option>
    <option>13</option>
    <option>13.5</option>
    <option>14</option>
    <option>14.5</option>
    <option>15</option>
    <option>15.5</option>
    <option>16.5</option>
</select>

<label for="admin">Bank's monthly admin fee</label>
<input type="text" name="admin" id="admin" value="57" disabled>

<label for="initiation">Finance initiation fee</label>
<input type="text" name="initiation" id="initiation" value="1140" disabled><br />

<input type="submit" value="Calculate">

</form>

<?php

if (isset($_POST['principle'])) $principleInput = $_POST['principle'];
if (isset($_POST['deposit'])) $depositInput = $_POST['deposit'];
if (isset($_POST['term'])) $termInput = $_POST['term'];
if (isset($_POST['interest'])) $interestInput = $_POST['interest'];
if (isset($_POST['balloon'])) $balloonInput = $_POST['balloon'];

$principleInputFinal = $principleInput + 1140;
echo "Finance Amount: " . $principleInputFinal;

//interest
$r = $interestInput / 12 / 100;
echo "<br /><br />Interest: " . $r;

//inputted balloon % in decimals
$br = $balloonInput / 100;
echo "<br /><br />Balloon Rate: " . $br;

//Balloon Amount
$ba = $principleInput * $br;
echo "<br /><br />Balloon Amount: " . $ba;

//Solve for x
$x1 = 1 + $r;

$x2 = pow($x1,-$termInput);

$x3 = 1 - $x2;

$x = $x3 / $r;
echo "<br /><br />" . $x;

//if balloon was selected, calculate new principle
$mpb = $principleInputFinal - $ba;
echo "<br /><br />New Principle Less Balloon: " . $mpb;

//deposit without balloon selected
$mpd = $principleInputFinal - $depositInput;
echo "<br /><br />Principle Less Deposit: " . $mpd;

//deposit with balloon selected
$mpdb = $mpb - $depositInput;
echo "<br /><br />Principle less balloon less deposit: " . $mpdb;

//no deposit and no balloon: calculate monthly installment on actual principle
$mp = $principleInputFinal / $x + 57;
echo "<br /><br />Installment on actual principle: " . $mp;

//interest payed on balloon amount.
$bar = $ba * $r;
echo "<br /><br />" . $bar;

//monthly installment less the interest payed for 
$mpbar = $mp - $bar;
echo "<br /><br />" . $mpbar;

//calculate monthly installment with no balloon but with deposit
$monthlyInstallmentDeposit = $mpd / $x + 57;
echo "<br /><br />Installment on principle less deposit: " . $monthlyInstallmentDeposit;

//calculate monthly installment with balloon but no deposit
$monthlyInstallmentBalloon = $mpb / $x + $bar + 57;
echo "<br /><br />Installment on principle less balloon, no deposit calculated: " . $monthlyInstallmentBalloon;

//calculate monthly installment with both balloon and deposit
$monthlyInstallmentBoth = $mpdb / $x + $bar + 57;
echo "<br /><br />Installment on principle less deposit and balloon" . $monthlyInstallmentBoth;



switch ($monthlyInstallment) {
    case ($principleInputFinal / $x + 57):
        echo "<br /><br />Installment on actual principle: " . $monthlyInstallment;
        break;

    case ($mpd / $x + $bar + 57):
        echo "<br /><br />Installment on principle less deposit: " . $monthlyInstallment;
        break;

    case ($mpb / $x + $bar + 57):
        echo "<br /><br />Installment on principle less balloon, no deposit calculated: " . $monthlyInstallment;
        break;

    case ($mpdb / $x + $bar + 57):
        echo "<br /><br />Installment on principle less deposit and balloon: " . $monthlyInstallment;
        break;
}

?>

Please don't get caught up in the math variables. The variables I need to print are:

The variables I want to print are: $mp , $monthlyInstallmentDeposit , $monthlyInstallmentBalloon , $monthlyInstallmentBoth .

This might be a dirty way to do it, but it works:

if (($depositInput > 0) && ($balloonInput > 0)) {
    echo "<br /><br />1: Installment on principle less deposit and balloon" . $monthlyInstallmentBoth;
} elseif (($depositInput > 0) && ($balloonInput <= 0)) {
    echo "<br /><br />2: Installment on principle less deposit: " . $monthlyInstallmentDeposit;
} elseif (($depositInput <= 0) && ($balloonInput > 0)) {
    echo "<br /><br />3: Installment on principle less balloon, no deposit calculated: " . $monthlyInstallmentBalloon;
} else {
    echo "<br /><br />4: Installment on actual principle: " . $mp;
}

This is my code for given parameter

<?php

// accepting form variable via post request
 if($_SERVER['REQUEST_METHOD']=='POST'){
      $amount = $_POST['amount'];
      $rate   = $_POST['rate'];
      $installments = $_POST['installments']; 
}

// if all good processing start
  if(!empty($amount) && !empty($rate) && !empty($installments)){

    $dayTime = date('D H:i');
    //$dayTime = "Fri 19:30";  //just for testing of if its friday and particular time between 15 to 20
    $day = explode(" ", $dayTime);  // to get the day from datetime string
    $time = explode(":", $day[1]);  // to get specific hours from time string
  //    var_dump($day[0]);die();

    // to check if its for special condition for base premium for 13%  
      if(($day[0] == "Fri") && (($time[0] >= 15) && ($time[0] <= 20))){ 

         $basePremiumfigure = 0.13;
         $commissionfigure  = 0.17;
         $taxfigure         = $rate /100;

         $basePremium = $amount * $basePremiumfigure;
         $commission  = $basePremium * $commissionfigure;
         $tax   = $basePremium * $taxfigure;
         $totalCost   = $basePremium + $commission + $tax;

       // calculating installments
           if ($installments > 1) {

             $installmentBasePremium = $basePremium / $installments;
             $installmentcommission  = $commission / $installments;
             $installmenttax         = $tax / $installments;
             $installmenttotalCost   = $totalCost / $installments;
           }


      }else{

         $basePremiumfigure = 0.11;
         $commissionfigure  = 0.17;
         $taxfigure         = $rate /100;

         $basePremium = $amount * $basePremiumfigure;
         $commission  = $basePremium * $commissionfigure;
         $tax   = $basePremium * $taxfigure;
         $totalCost   = $basePremium + $commission + $tax;

            // calculating installments
           if ($installments > 1) {

             $installmentBasePremium = $basePremium / $installments;
             $installmentcommission  = $commission / $installments;
             $installmenttax         = $tax / $installments;
             $installmenttotalCost   = $totalCost / $installments;
           }
      }


  }

?>

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<link href="style.css" rel="stylesheet" id="bootstrap-css">


<div class="container">
      <div class="price-box">
         <a href="/insly/insurance/insurance.php"><center><h3>Car Insurance Calculator</h3></center></a>
        <div class="row">
            <div class="col-sm-12">

                 <form class="form-horizontal form-pricing" role="form" action="calculate.php" method="Post">

                      <div class="price-slider">    
                         <h4 class="great">Value</h4>

                            <div class="col-sm-12">
                                 <div class="form-group">
                                  <input type="text" value="<?php echo $amount; ?>" readonly>
                                 </div>
                            </div>


                      </div>                  

              <div class="price-slider">    
                    <h4 class="great">Base Premium (<?php echo $basePremiumfigure*100; ?>%)</h4>

                          <div class="col-sm-12">
                                 <div class="form-group">
                                  <input type="text" value="<?php echo $basePremium; ?>" readonly>
                                 </div>
                            </div>

              </div>               

              <div class="price-slider">
                    <h4 class="great">Commission (17%)</h4>

                     <div class="col-sm-12">
                                 <div class="form-group">
                                  <input type="text" value="<?php echo $commission; ?>" readonly>
                                 </div>
                            </div>

              </div> 

              <div class="price-slider">               
                    <h4 class="great">Tax (<?php echo $rate; ?>%)</h4>

                         <div class="col-sm-12">
                                 <div class="form-group">
                                  <input type="text" value="<?php echo $tax; ?>" readonly>
                                 </div>
                            </div>

              </div>                

               <div class="price-slider"> 
                    <h4 class="great">Total Amount</h4>

                        <div class="col-sm-12">
                                 <div class="form-group">
                                  <input type="text" value="<?php echo $totalCost; ?>" readonly>
                                 </div>
                            </div>

               </div>


                 <table class="table">
                        <thead>

                          <tr>
                            <?php $i = 1; 
                                while($i <= $installments){
                             ?>
                            <th>installment<?php echo $i; ?> </th>
                            <th>Base Premium</th>
                            <th>Commission</th>
                            <th>Tax</th>
                            <th>Total Coast</th>
                            <?php $i++; ?>
                          </tr>
                        </thead>
                        <tbody>
                            <tr>
                           <td></td>
                          <?php echo "<td>". number_format($installmentBasePremium,2)."</td>"; ?>
                          <?php echo "<td>". number_format($installmentcommission,2)."</td>"; ?>
                          <?php echo "<td>". number_format($installmenttax,2)."</td>"; ?>
                          <?php echo "<td>". number_format($installmenttotalCost,2)."</td>"; ?>
                             </tr>
                         <?php } ?>
                        </tbody>
                </table>    

                 </form>    

        </div>
      </div>
</div> 

view my full code on below link.

You can try this script on my website.

https://ansariazam72.blogspot.com/2018/09/basic-car-insurance-calculator.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM