简体   繁体   English

基本的PHP表单帮助(2)

[英]Basic php form help (2)

This is an extension of a question I had yesterday. 这是我昨天提出的问题的扩展。

I am trying to make a little php calculator that will show how much people can save on their phone bills if they switch to VoIP, and how much they can save with each service. 我正在尝试制作一个小的php计算器,该计算器将显示如果人们切换到VoIP可以节省多少电话费用,以及每种服务可以节省多少费用。

I have a form that will spit out the right amount for a monthly bill here: 我有一张表格,可以在这里按月支付正确的费用:

http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=Submit http://www.nextadvisor.com/voip_services/voip_calculator.php?monthlybill=50&Submit=提交

But now I need to integrate that with some other data and put in a table. 但是现在我需要将其与其他一些数据集成在一起并放在表格中。 The prices for each of the different services are in another file called "values.php". 每个不同服务的价格都在另一个名为“ values.php”的文件中。 The values are: 值是:

 $offer1calcsavings="24.99";
 $offer2calcsavings="20.00";
 $offer3calcsavings="21.95";
 $offer4calcsavings="23.95";
 $offer5calcsavings="19.95";
 $offer6calcsavings="23.97";
 $offer7calcsavings="24.99";

I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value. 我希望表的七行中的每一行都具有从monthlybill值中减去的offercalc Savings值之一。

The php code currently looks like this: 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};

   }
   ?>

You can include the values.php file like this: 您可以像这样包含values.php文件:

include 'path/to/values.php';

If you put the values in values.php in an array you can easily loop through them using a foreach loop: 如果将值放在一个数组中的values.php中,则可以使用foreach循环轻松遍历它们:
in values.php; 在values.php;中

$values['1calsavings'] = 24.99;
$values['2calsavings'] = 20.00;
etc;

Then in the other file: 然后在另一个文件中:

include 'path/to/values.php';
foreach($values as $name => $amount){
 echo '<some_markup>';
 echo "$name $amount";
 echo '</some_markup>'; 
}

Egads. 哎呀

Use arrays! 使用数组!

So: 所以:

$offercalcsavings[0] = "24.99";  
$offercalcsavings[1] = "20.00";  
etc. etc.

Then, you're looping the output really: 然后,您实际上是在循环输出:

for($i = 0; $i < CONSTANT_THAT_EQUALS_7; $i++) {
    echo "<html bits>";
    echo $offercalcsavings[$i];
    echo $offerlink[$i];
    etc. etc.
}

I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value. 我希望表的七行中的每一行都具有从monthlybill值中减去的offercalc Savings值之一。

You need to do the math somewhere. 您需要在某处进行数学运算。 Presumably you would do this before the echo statement where you output your row. 大概是在输出行的echo语句之前执行此操作。

$offerwithsavings = $monthlybill - $offercalcsavings

Then just be sure to include it in your table somewhere. 然后只需确保将其包括在表中的某个位置即可。

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

The real prescription for this code is an array and foreach() loop, but I thought I'd keep my answer simple for now. 这段代码的真正处方是一个数组和foreach()循环,但是我认为我现在暂时保持简单。 Arrays and foreach() loops are very powerful and relatively quick and easy to learn. 数组和foreach()循环功能非常强大,并且相对较快且易于学习。 You would do well to give them some in depth study. 您最好对他们进行深入研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM