Help with decimal Places

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
]{ronic
Forum Commoner
Posts: 27
Joined: Thu Mar 04, 2004 10:42 pm

Help with decimal Places

Post by ]{ronic »

Hey,

Im no php guru, just a newbie ...

my data is stored in excel file .. I use a programe to convert it to a sql file which is all good .. the problem I am having is in excel, the price cell is formated to show $ + 0 decimal places .. when it is converted to a sql file it drops the formating to no symbol and 1 decimal place ...

Is it posible to force it to 0 decimals so it is just 10 rather than 10.3?

Also an if statement so if there is no data in the mysql price cell .. return blank (so that cell has no data printed).

Any help on the intergration of the above would be great as I have tried a few ideas with no luck .. but again I am a newbie.


Below is a copy of my script:

Code: Select all

for ($i = 0; $i < $numofrows; $i++) &#123;
$row = mysql_fetch_assoc($result);
if ($i % 2) &#123;
echo "<TR bgcolor="#CCCCCC">\n";
&#125; else &#123;
echo "<TR bgcolor="#0099CC">\n";
&#125;
echo "<TD>".$row&#1111;'description']."</TD><TD align="right">\$".$row&#1111;'price']."</TD><TD>";

$image = $row&#1111;'link'];

if ($image)
echo "<a href="$image" target="new"><strong><center>Link</center></strong></a>";
else
echo "<center></center>";

echo "</TD></TR>\n";

Thanks

]{ronic
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

[php_man]round[/php_man]

Code: Select all

//........skipped.....
echo "<TD>".$row['description']."</TD><TD align="right">\$".round($row['price'])."</TD><TD>";
//.........skipped
]{ronic
Forum Commoner
Posts: 27
Joined: Thu Mar 04, 2004 10:42 pm

Post by ]{ronic »

Hey Weirdan,

Thanks for that .. works like a charm.

I have been trying to get an "if" function in there as well .. So if the data base holds no records in the "price" cell display nothing rather than $0

Any ideas on that .. how I have tried it is leaving out the cell totally for the rows that have no data in it.


Thanks

]{ronic
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

//........skipped..... 
if(round($row['price']))
  $price_field=round($row['price']);
else $price_field="";
echo "<TD>".$row['description']."</TD><TD align="right">\$".$price_field."</TD><TD>"; 
//.........skipped
]{ronic
Forum Commoner
Posts: 27
Joined: Thu Mar 04, 2004 10:42 pm

Post by ]{ronic »

Hey ..

Thanks dewd that does it .. except that the $ stills displays in the empty cell .. Is the way the $ is incorporated a plausable way to do it?

Thanks

]{ronic
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

sure:

Code: Select all

//........skipped..... 
if(round($row['price'])) 
  $price_field="\$".round($row['price']); 
else $price_field=""; 
echo "<TD>".$row['description']."</TD><TD align="right">".$price_field."</TD><TD>"; 
//.........skipped
]{ronic
Forum Commoner
Posts: 27
Joined: Thu Mar 04, 2004 10:42 pm

Post by ]{ronic »

Hey Weirdan,

That does the job perfectly .. Thank you very much for your input/time

]{ronic
Post Reply