views:

102

answers:

3

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

   }
   ?>
+1  A: 

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>'; 
}
Pim Jager
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.
pg
+1  A: 

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.
}
sobedai
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.
pg
Too bad I can only vote up once. +1000 for "Egads!"
Noah Goodrich
+1  A: 

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.

Thank you mechler!
pg
pg