【问题标题】:Display a variable based on text input and selections in php根据 php 中的文本输入和选择显示变量
【发布时间】:2014-04-08 20:32:31
【问题描述】:

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

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

通常情况下,用户不会填写所有字段。以下是条件:

  1. 用户填写所有字段 ($monthlyInstallmentBoth)
  2. 用户完成购买价格、存款、利息和期限(未选择气球)($monthlyInstallmentDeposit)
  3. 用户完成购买价格、利息、气球和期限(未选择存款)($monthlyInstallmentBalloon)
  4. 用户完成购买价格、利息和期限(未选择存款或气球)($mp)

假设变量满足以上条件:

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

这是我的问题:如何在选择所有选项时仅显示 $monthlyInstallmentBoth 或在未选择气球百分比时仅显示 $monthlyInstallmentDeposit 或仅显示 $monthlyInstallmentBalloon 如果如果选择了没有存款和没有气球百分比,则没有选择存款或仅显示$mp

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

请看我下面的代码:

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

【问题讨论】:

    标签: php conditional-statements calculator


    【解决方案1】:

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

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

    【讨论】:

      【解决方案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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-27
        • 1970-01-01
        • 1970-01-01
        • 2020-09-18
        相关资源
        最近更新 更多