Forum Moderators: coopster

Message Too Old, No Replies

Number format in echo statement of a row

         

oceanwave

3:12 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



Hi,

Items are being displayed after query of database as in the format (looping - rows):

echo "Price = $row[price]   ....

How do you use the number format function inside an echo statement referring to rows? I want to add commas to my price values in each row displayed.
12345 would change to 12,345, etc.

I've used the number format before, but never in a looping row echo statement.

jatar_k

3:26 pm on Sep 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



echo 'Price = ',number_format($row['price']),'  ';

though you would have to add the extra parameters for the function

[edited by: jatar_k at 3:26 pm (utc) on Sep. 7, 2007]

d40sithui

3:26 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



try

<?

$decimal_places = 2;

echo "price ". number_format($row['price'], $decimal_places). "&nbsp;&nbsp;";

?>
careful though, number_format() rounds up numbers if you dont use the optional parameters.
also you can try money_format.

[us3.php.net...]
[us3.php.net...]

mooger35

3:29 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



Try this

echo "Price = ".number_format($row['price'],2)." &nbsp;&nbsp;....";

//edit: just a little late with the submit button.

d40sithui

4:01 pm on Sep 7, 2007 (gmt 0)

10+ Year Member



lol and i thought i was late

oceanwave

12:31 am on Sep 8, 2007 (gmt 0)

10+ Year Member



Thank you everyone. Works fine now. Just out of curiosity....where did you find/learn the code for this?

jatar_k

1:37 am on Sep 8, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



pretty standard, you can mix functions into almost anywhere, make single lines of code be as confusing as possible

though it isn't always recommended as it becomes hard to follow at some point

you could do this

$mytexttoinsert = mysql_real_escape_string(strip_tags(trim($_POST['somevalue'])));

I have one somewhere with quite a few more than that but couldn't find it for an example

and the way to learn new functions, is to read the manual