views:

491

answers:

4

You guys were very helpful yesterday. I am still a bit confused here though.

I want to make it so that the numbers on the rightmost column are rounded off to the nearest dollar:

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

the code for the table looks like this:

I want $offer[1,2,3,4,5,6,7]calcsavingsann to be rounded, how can do this?

 <table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >

    <tr class="credit_table2_brd">
     <td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Monthly VoIP Bill:</td>
<td class="credit_table2_brd_lbl" width="155px;">Annual Savings:</td>

   </tr>  

   <?php

 $offer1price="24.99";
 $offer2price="20.00";
 $offer3price="21.95";
 $offer4price="23.95";
 $offer5price="19.95";
 $offer6price="23.97";
 $offer7price="24.99";

 $offer1calcsavings= $monthlybill - $offer1price;
 $offer2calcsavings= $monthlybill - $offer2price;
 $offer3calcsavings= $monthlybill - $offer3price;
 $offer4calcsavings= $monthlybill - $offer4price;
 $offer5calcsavings= $monthlybill - $offer5price;
 $offer6calcsavings= $monthlybill - $offer6price;
 $offer7calcsavings= $monthlybill - $offer7price;

 $monthybill="monthlybill";

 $offer1calcsavingsann= $offer1calcsavings * 12;
 $offer2calcsavingsann= $offer2calcsavings * 12;
 $offer3calcsavingsann= $offer3calcsavings * 12;
 $offer4calcsavingsann= $offer4calcsavings * 12;
 $offer5calcsavingsann= $offer5calcsavings * 12;
 $offer6calcsavingsann= $offer6calcsavings * 12;
 $offer7calcsavingsann= $offer7calcsavings * 12;

   $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};
    $offerpr ='offer'.$re.'price';
$offerprice=${$offerpr};
$offersavann ='offer'.$re.'calcsavingsann';
$offercalcsavingsann=${$offersavann};

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 >$'.$offerprice.'</td>
     <td >$'.$offercalcsavingsann.'</td>


   </tr>';
   $re=$re+1;
   $offer ='offer'.$re.'name';
 $offername= ${$offer};

   }



   ?>



 </table>
+4  A: 

Do you want rounded up/down/truncated to the nearest dollar?

Here are some suggested functions you can use:

Rounding

round

floor

ceil

Formatting/Truncating

sprintf

grepsedawk
+4  A: 

Grepsedawk's answer is good; the only thing I would add is that rather than displaying $336.6, for example, you could could use number_format to output $336.60.

(I know this wasn't your question, but looking at the link, I thought that might be useful for you.)

Edit - Thanks to Andy for suggesting money_format instead.

John McCollum
money_format would be better for currency formatting, though.
Andy
Thanks for the addition, duly noted. :)
John McCollum
Yes but where do I put "numbe_format"? I'm not sure of the usage. Do I put it after the first "echo" or before the individual value?
pg
A: 

money_format() is a function that returns a string value of a formatted number. You have control over the formatting and, obviously, your number. A simple example, if you have your value in the variable $myNumber, you could incorporate the result into a given table's data cell like so;

<?php echo ("<td>".money_format('%n',$myNumber)."</td>"); ?>

And you would need to do this for every value, e.g. via a for loop if you had all your values in an array. The n here is one of the formatting options - there are several. A good place to look would be on the PHP web page at http://au2.php.net/manual/en/function.money-format.php Hope this helps.

Joe
A: 

I can't seem to get the usage right. The way I am using echo is

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 >$'.$offerprice.'</td>

<td >$'.$offercalcsavingsann.'</td>


   </tr>';

I put the "set locale" up where the

 "<?php"

is. I don't understand how to write it, and every way I do just returns an error.

pg