Jump to content

using substr to trim 00 off variable


phpnewbie88

Recommended Posts

Hello,

 

I am trying to use the php function string substr ( string $string , int $start [, int $length ] ) to remove trailing zero's in the DB from the price displayed on the page.

 

Currently using the code below I get this for output - $27.9500

 

<?php echo $row['prodprice']; ?>

 

I would like to get this $27.95.

 

So I tried using the string substr ( string $string , int $start [, int $length ] ) like this -

 

<?php echo substr("$row['prodprice']",0,5); ?>

 

but I receive t string errors and such using variations of this.

 

Can someone please explain to me what I am doing wrong and give me some pointers on how to fix this?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/216581-using-substr-to-trim-00-off-variable/
Share on other sites

If it's is stored as a decimal value in the database (it really should be), you can use ROUND() in the query

 

SELECT ROUND(`prodprice`, 2) AS price FROM `table`

 

The value would then be in $row['price']

$result = mysql_query("SELECT * FROM isc_products,isc_product_images
WHERE productid='537'") or die(mysql_error());  

right now i am using this, how would I implement your code suggestion?

 

If it's is stored as a decimal value in the database (it really should be), you can use ROUND() in the query

 

SELECT ROUND(`prodprice`, 2) AS price FROM `table`

 

The value would then be in $row['price']

This should work for you. Then you'd just need to change your echos to reflect $row['price'] instead of $row['prodprice'], and get rid of all the substr() usage.

 

SELECT *, ROUND(`prodprice`, 2) AS price FROM isc_products,isc_product_images WHERE productid='537'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.