2008-12-06 9 views
0

Vous avez été très utile hier. Je suis encore un peu confus ici.aide de base pour les formulaires php (affichage des devises)

Je veux faire en sorte que les chiffres sur la colonne de droite sont arrondies au dollar le plus proche:

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

le code de la table ressemble à ceci:

Je veux $ offre [1,2,3,4,5,6,7] calcsavingsann être arrondi, comment peut-on faire?

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

Répondre

5

Voulez-vous arrondi à la ligne supérieure/inférieure/tronquée au dollar le plus près?

Voici quelques fonctions proposées, vous pouvez utiliser:

Arrondi

round

floor

ceil

Mise en forme/Tronquer

sprintf

4

La réponse de Grepsedawk est bonne; la seule chose que j'ajouterais, c'est qu'au lieu d'afficher 336,6 $, par exemple, vous pourriez utiliser number_format pour générer 336,60 $.

(Je sais que ce n'était pas votre question, mais en regardant le lien, je pensais que cela pourrait vous être utile.)

Edit - Merci à Andy pour suggérer money_format à la place.

+0

money_format serait mieux pour le formatage des devises, cependant. – Andy

+0

Merci pour l'ajout, dûment noté. :) –

+0

Oui mais où est-ce que je mets "numbe_format"? Je ne suis pas sûr de l'utilisation. Est-ce que je le mets après le premier "écho" ou avant la valeur individuelle? –

0

money_format() est une fonction qui renvoie une valeur de chaîne d'un nombre formaté. Vous avez le contrôle sur le formatage et, évidemment, votre numéro. Un exemple simple, si vous avez votre valeur dans la variable $myNumber, vous pouvez incorporer le résultat dans la cellule de données d'une table donnée comme si;

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

Et vous devez le faire pour chaque valeur, par exemple via une boucle for si vous aviez toutes vos valeurs dans un tableau. Le n est l'une des options de formatage - il y en a plusieurs. Un bon endroit pour regarder serait sur la page Web de PHP au http://au2.php.net/manual/en/function.money-format.php Espérons que cela aide.

0

Je n'arrive pas à obtenir l'utilisation correcte. La façon dont je me sers d'écho est

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

je mets la "locale set" là où le

"<?php" 

est. Je ne comprends pas comment l'écrire, et chaque fois que je fais juste renvoie une erreur.