0

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.

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

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". 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.

The php code currently looks like this:

 <?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};
 }
 ?>
Brian Tompsett - 汤莱恩
5,92772 gold badges64 silver badges135 bronze badges
asked Dec 4, 2008 at 17:18
1
  • You should actually ask your question in your question (see your comment to my answer) 'I want "$offer1(2,3,4,5,6,7)calsavings" to be subtracted from "$monthlybill"' Commented Dec 4, 2008 at 18:04

3 Answers 3

1

I want each of the seven rows of the table to have one of the offercalcsavings values subtracted from the monthlybill value.

You need to do the math somewhere. Presumably you would do this before the echo statement where you output your row.

$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. Arrays and foreach() loops are very powerful and relatively quick and easy to learn. You would do well to give them some in depth study.

answered Dec 4, 2008 at 18:00
Sign up to request clarification or add additional context in comments.

1 Comment

I made it so the values look like this: $offer1calcsavings="$monthlybill - $offer1price * 12"; but it just spits out the equation rather than doing the math, as seen here: nextadvisor.com/voip_services/…
1

You can include the values.php file like this:

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:
in 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>'; 
}
answered Dec 4, 2008 at 17:31

1 Comment

Wait, I can make the different offercalcsavings variables show up just fine, but I can't get it to do the arithmetic, I want "$offer1(2,3,4,5,6,7)calsavings" to be subtracted from "$monthlybill" so I can show what they could save from various different providers.
1

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.
}
answered Dec 4, 2008 at 17:35

1 Comment

Wait, I can make the different offercalcsavings variables to show up just fine, but I can't get it to to the arithmetic, I want that number to be subtracted from "$monthlybill" so I can show what they could save from various different providers.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.