[英]Basic php form help (2)
這是我昨天提出的問題的擴展。
我正在嘗試制作一個小的php計算器,該計算器將顯示如果人們切換到VoIP可以節省多少電話費用,以及每種服務可以節省多少費用。
我有一張表格,可以在這里按月支付正確的費用:
http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=提交
但是現在我需要將其與其他一些數據集成在一起並放在表格中。 每個不同服務的價格都在另一個名為“ values.php”的文件中。 值是:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
我希望表的七行中的每一行都具有從monthlybill值中減去的offercalc Savings值之一。
php代碼當前如下所示:
<?php $monthlybill = $_GET['monthlybill']; ?>
Your monthly bill was <?php echo "$monthlybill"; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!=""){
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td ><span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="http://www.nextadvisor.com'.$offerstar.'" alt="" />
</span><br />
<div style="margin-top:5px; color:#0000FF;">
<a href="'.$offerlink.'" target="blank">Go to Site</a>
<span style="margin:0px 7px 0px 7px;">|</span>
<a href="'.$offeranchor.'">Review</a>
</div> </td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
您可以像這樣包含values.php文件:
include 'path/to/values.php';
如果將值放在一個數組中的values.php中,則可以使用foreach循環輕松遍歷它們:
在values.php;中
$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;
然后在另一個文件中:
include 'path/to/values.php';
foreach($values as $name => $amount){
echo '<some_markup>';
echo "$name $amount";
echo '</some_markup>';
}
哎呀
使用數組!
所以:
$offercalcsavings[0] = "24.99";
$offercalcsavings[1] = "20.00";
etc. etc.
然后,您實際上是在循環輸出:
for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
echo "<html bits>";
echo $offercalcsavings[$i];
echo $offerlink[$i];
etc. etc.
}
我希望表的七行中的每一行都具有從monthlybill值中減去的offercalc Savings值之一。
您需要在某處進行數學運算。 大概是在輸出行的echo語句之前執行此操作。
$offerwithsavings = $monthlybill - $offercalcsavings
然后只需確保將其包括在表中的某個位置即可。
<td >'.$offerwithsavings.'</td>
這段代碼的真正處方是一個數組和foreach()循環,但是我認為我現在暫時保持簡單。 數組和foreach()循環功能非常強大,並且相對較快且易於學習。 您最好對他們進行深入研究。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.