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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.