phpnewbie88 Posted October 22, 2010 Share Posted October 22, 2010 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 More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 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'] Link to comment https://forums.phpfreaks.com/topic/216581-using-substr-to-trim-00-off-variable/#findComment-1125278 Share on other sites More sharing options...
phpnewbie88 Posted October 22, 2010 Author Share Posted October 22, 2010 $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'] Link to comment https://forums.phpfreaks.com/topic/216581-using-substr-to-trim-00-off-variable/#findComment-1125282 Share on other sites More sharing options...
Pikachu2000 Posted October 22, 2010 Share Posted October 22, 2010 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' Link to comment https://forums.phpfreaks.com/topic/216581-using-substr-to-trim-00-off-variable/#findComment-1125286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.