我在使用这个长源代码时遇到问题。是否有任何替代代码或条件声明来缩短或最小化代码。我使用if-ElseIf语句。谢谢!
我实际上在关于指定金额的多个条件上做了一个系统。防爆。如果SHARE等于或小于30,000,则它们是使用不同月份计算的指定百分比。
<div class="span8" align="center">
<table width="100%">
<tr>
<td align="center" width="50%">
<form action="" method="POST">
<label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
<br/>
<label>Share Capital:</label>
<input onBlur="this.value=formatCurrency(this.value)" type="hidden">
<input type="text" name="share" value="0" class="textbox"/><br>
<label>Term:</label>
<select name="terms"/>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
</select> <br />
<input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
</form></td>
<td width="50%" align="justify" >
<div class="alert alert-info" align="justify" style="font-size:16px">
<?php if(isset($_POST['calculate'])) {
$share = $_POST['share'];
$terms = $_POST['terms'];
?>
<?php if($share <= "30000"){ ?>
<?php if($terms == "1"){
$interest = $share * .0130;
$service_fee = $share * .01;
$filling_fee = 30;
$cash_on_hand = $share - ($service_fee + $interest + $filling_fee);
$loan_receivable = $cash_on_hand + $service_fee + $interest + $filling_fee;
$debit = $loan_receivable;
$credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
<td width="30%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
<td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
</tr>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td><br/><br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $terms. " month "; ?> </td>
</tr>
</table>
<?php } elseif($terms == "2"){
$interest = $share * .0195;
$service_fee = $share * .01;
$filling_fee = 30;
$cash_on_hand = $share - ($service_fee + $interest + $filling_fee);
$loan_receivable = $cash_on_hand + $service_fee + $interest + $filling_fee;
$debit = $loan_receivable;
$credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
<td width="30%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
<td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
</tr>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td><br/><br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $terms. " months "; ?> </td>
</tr>
</table>
<!--END OF SHARE 30,000 BELOW --> <?php } ?>
<!--START OF SHARE 30,000 ABOVE --> <?php } elseif($share >= "30001"){ ?>
<?php if($terms == "1"){
$interest = $share * .0130;
$service_fee = $share * .01;
$filling_fee = 30;
$cash_on_hand = $share - ($service_fee + $interest + $filling_fee);
$loan_receivable = $cash_on_hand + $service_fee + $interest + $filling_fee;
$debit = $loan_receivable;
$credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
<td width="30%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
<td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
</tr>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td><br/><br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $terms. " month "; ?> </td>
</tr>
</table>
<?php } elseif($terms == "2"){
$interest = $share * .0205;
$service_fee = $share * .01;
$filling_fee = 30;
$cash_on_hand = $share - ($service_fee + $interest + $filling_fee);
$loan_receivable = $cash_on_hand + $service_fee + $interest + $filling_fee;
$debit = $loan_receivable;
$credit = $interest + $service_fee + $filling_fee + $cash_on_hand;
?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
<td width="30%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
<td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
</tr>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td><br/><br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $terms. " months "; ?> </td>
</tr>
</table>
<?php } ?>
<?php } ?>
<?php } ?>
</div> </td>
</tr>
</table>
答案 0 :(得分:1)
注意:使用上一个链接并缩短了代码。请正确测试,只做空白编码
<div class="span8" align="center">
<table width="100%">
<tr>
<td align="center" width="50%">
<form action="" method="POST">
<label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
<br/>
<label>Share Capital:</label>
<input onBlur="this.value = formatCurrency(this.value)" type="hidden">
<input type="text" name="share" value="0" class="textbox"/><br>
<label>Term:</label>
<select name="terms"/>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
</select> <br />
<input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
</form></td>
<td width="50%" align="justify" >
<div class="alert alert-info" align="justify" style="font-size:16px">
<?php
if (isset($_POST['calculate'])) {
$share = $_POST['share'];
$terms = $_POST['terms'];
?>
<?php if ($share <= "30000") { ?>
<?php
if ($terms == "1") {
$interest = $share * .0130;
} elseif ($terms == "2") {
$interest = $share * .0195;
if ($terms == "1") {
$interest = $share * .0130;
} elseif ($terms == "2") {
$interest = $share * .0205;
}}
elseif($ share&gt; = 30001){
if($ terms ==&#34; 1&#34;){
$ interest = $ share * .0130;
} elseif($ terms ==&#34; 2&#34;){
$ interest = $ share * .0205;
}
}
?&GT;
?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($loan_receivable, 2); ?> </td>
<td width="30%" align="right"> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($interest, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($service_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($filling_fee, 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"> </td>
<td width="30%" align="right"><?php echo number_format($cash_on_hand, 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($debit, 2); ?></td>
<td width="30%" align="right"><?php echo number_format($credit, 2); ?></td>
</tr>
<tr>
<td><br/><br/></td>
<td><br/><br/></td>
<td><br/><br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($share, 2); ?> </td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $terms . " month "; ?> </td>
</tr>
</table>
<?php } ?>
</div> </td>
</tr>
</table>
答案 1 :(得分:1)
我认为你在这里可以做的是创建一个函数(或者将其作为保存在类中的一组工具的一部分),您可以通过include()
使其可以重用。将算法混合到您的HTML中将严重限制其可能在其他地方使用。当你散布它时,它也会混乱你的html输出。创建函数/ class-&gt;方法允许扩展并移植到其他函数/ class-&gt;方法中:
<?php
// Create a function that can accept parameters
function CalculateItem($share = 0,$terms = 0)
{
if($share <= 30000)
$multiplier = ($terms == "1")? .0130 : .0195;
elseif($share >= 30001)
$multiplier = ($terms == "1")? .0130 : .0205;
if(empty($multiplier))
return;
$data['share'] = $share;
$data['terms'] = $terms;
$data['interest'] = ($share * $multiplier);
$data['service_fee'] = ($share * .01);
$data['filling_fee'] = 30;
$data['cash_on_hand'] = $share - ($data['service_fee'] + $data['interest'] + $data['filling_fee']);
$data['loan_receivable'] = $data['cash_on_hand'] + $data['service_fee'] + $data['interest'] + $data['filling_fee'];
$data['debit'] = $data['loan_receivable'];
$data['credit'] = $data['interest'] + $data['service_fee'] + $data['filling_fee'] + $data['cash_on_hand'];
return $data;
}
// Get the state of data. Anything but false will trigger the breakout table
$data = (isset($_POST['calculate']))? CalculateItem($_POST['share'],$_POST['terms']) : false;
?>
<div class="span8" align="center">
<table width="100%">
<tr>
<td align="center" width="50%">
<form action="" method="POST">
<label> *NOTE: Php.36,000.00 - maximum applied loanable amount</label>
<br/>
<label>Share Capital:</label>
<input onBlur="this.value=formatCurrency(this.value)" type="hidden">
<input type="text" name="share" value="0" class="textbox"/>
<br>
<label>Term:</label>
<select name="terms"/>
<option value="1">1 Month</option>
<option value="2">2 Months</option>
</select>
<br />
<input type="submit" name="calculate" class="btn btn-success" value="Let's COMPUTE">
</form></td>
<td width="50%" align="justify" >
<div class="alert alert-info" align="justify" style="font-size:16px">
<?php if($data != false) { ?>
<table width="100%">
<tr>
<td width="40%" align="justify"><?php echo "Loan Receivable"; ?></td>
<td width="30%" align="right"><?php echo number_format($data['loan_receivable'], 2); ?></td>
<td width="30%" align="right"></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Interest "; ?></td>
<td width="30%" align="right"></td>
<td width="30%" align="right"><?php echo number_format($data['interest'], 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Service Fee "; ?></td>
<td width="30%" align="right"></td>
<td width="30%" align="right"><?php echo number_format($data['service_fee'], 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Filling Fee "; ?></td>
<td width="30%" align="right"></td>
<td width="30%" align="right"><?php echo number_format($data['filling_fee'], 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Cash On Hand "; ?></td>
<td width="30%" align="right"></td>
<td width="30%" align="right"><?php echo number_format($data['cash_on_hand'], 2); ?></td>
</tr>
<tr bgcolor="#CCCCCC" style="color:#000000; font-weight:bold">
<td width="40%" align="right"><?php echo "TOTAL "; ?></td>
<td width="30%" align="right"><?php echo number_format($data['debit'], 2); ?></td>
<td width="30%" align="right"><?php echo number_format($data['credit'], 2); ?></td>
</tr>
<tr>
<td><br/>
<br/></td>
<td><br/>
<br/></td>
<td><br/>
<br/></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Applied Loan "; ?></td>
<td width="30%" align="right"><?php echo number_format($data['share'], 2); ?></td>
</tr>
<tr>
<td width="40%" align="justify"><?php echo "Term: "; ?></td>
<td width="30%" align="right"><?php echo $data['terms']. " month "; ?></td>
</tr>
</table>
<?php } ?>
</div>
</td>
</tr>
</table>