2008-12-04 81 views
0

这是我昨天的一个问题的延伸。基本的PHP窗体帮助(2)

我想做一个小小的php计算器,它将显示出多少人可以节省电话费,如果他们切换到VoIP,以及他们可以节省多少服务。

我有一个表格,将吐出适量这里每月账单:

http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=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值减去offercalcsavings值之一。

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

    } 
    ?> 
+0

实际上你应该在你的问题中提出你的问题(请参阅你对我的回答的评论)'我要'$ offer1(2,3,4,5,6,7)calsavings“要从”$ monthlybill“中减去'' – 2008-12-04 18:04:26

回答

1

我希望每个七行的 表的有 offercalcsavings之一值从每月的价值中扣除 。

你需要在某个地方做数学。大概你会在输出你的行的echo语句之前这样做。

$offerwithsavings = $monthlybill - $offercalcsavings 

然后只要确保将它包含在表中某处。

<td >'.$offerwithsavings.'</td> 

此代码的良方是一个数组,foreach()循环,但我想我会保持我的回答简单的现在。数组和foreach()循环非常强大,相对快速且易于学习。你会做得很好,给他们一些深入的研究。

1

可以包括values.php文件是这样的:

include 'path/to/values.php'; 

如果你把值values.php数组中的你可以使用foreach循环轻松遍历它们:
in 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>'; 
} 
+0

等等,我可以使不同的offercalcsavings变量显示出来很好,但我不能让它做算术,我想要“$ offer1(2,3,4,5,6,7)calsavings”被减去从“$ monthlybill”,所以我可以显示他们可以从各种不同的提供商保存。 – 2008-12-04 17:55:00

1

Egads。

使用数组!

所以:

$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. 
} 
+0

等等,我可以使不同的offercalcsavings变量显示出来很好,但我无法达到算术,我想从“$ monthlybill”中减去这个数字,以便我可以显示他们可以从各种各样的节省不同供应商。 – 2008-12-04 17:53:55

+0

太糟糕了,我只能投一次。 “Egads!”+1000 – 2008-12-04 18:09:34