根据文本输入和ph​​p中的选择显示变量

时间:2014-04-08 20:32:32

标签: php conditional-statements calculator

我目前正在开发PHP的汽车贷款计算器。用户输入为:车辆购买价格,存款,利率,气球百分比,贷款期限(月)和两个必填字段。

我的数学工作100%,如果你运行我的脚本,你将会见证这一点,因为我已经回应了所有的答案。数学不是问题。

用户通常不会完成所有字段。以下是条件:

  1. 用户完成所有字段($monthlyInstallmentBoth
  2. 用户完成购买价格,存款,利息和期限(未选择气球)($monthlyInstallmentDeposit
  3. 用户完成购买价格,利息,气球和期限(未选择存款)($monthlyInstallmentBalloon
  4. 用户完成购买价格,利息和期限(未选择存款或气球)($mp
  5. 假设具有上述条件的变量:

    1. $monthlyInstallmentBoth
    2. $monthlyInstallmentDeposit
    3. $monthlyInstallmentBalloon
    4. $mp
    5. 以下是我的问题:如果选择了所有选项,我该如何仅显示$monthlyInstallmentBoth;如果选择了 <气球百分比,则仅显示$monthlyInstallmentDeposit {1}}如果没有选择存款,如果选择没有存款且没有气球百分比,则仅显示$monthlyInstallmentBalloon

      我尝试了一个switch语句,但我不确定这是我目前需要的。因为它不起作用。

      请参阅下面的代码:

      $mp

      请不要陷入数学变量。我需要打印的变量是:

      我要打印的变量有:<?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; } ?> $mp$monthlyInstallmentDeposit$monthlyInstallmentBalloon

2 个答案:

答案 0 :(得分:0)

这可能是一种肮脏的方式,但它有效:

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

答案 1 :(得分:-2)

这是我给定参数的代码

<?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> 

在下面的链接上查看我的完整代码。

您可以在我的网站上尝试使用此脚本。

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