PDA

View Full Version : formatting a record to a number - newbie!



steveh
08-12-2009, 05:15 PM
Hi all,

Im very new to programming in general but have managed to setup a MSQL database, Wamp Server and a website which can add, edit data and connect with the database (i know not very difficult!)

What i wanted to do now is start to clean up the data and format it in order for it to really make sense.

Below is what i currently have for one of the records which is a number, which i want to be formatted. For example 3400, would show as 3,400. I know this is probably very simple but whilst searching the net i could not get a definitive answer for doing this.

[<td><input type="number" name="limit" size="32" /></td>]

Thank you in advance

Steve

Schmoopy
08-12-2009, 06:06 PM
Here's a function that should do the trick:



<?php
$num = 3400;
echo number_format($num);

// Output - 3,400
?>


Use number_format and then replace ($num) with the data from the record.

Hope this helps.

steveh
08-12-2009, 07:30 PM
thank you for coming back to me, i understand what you have said and that is great but is it possible to make the format work for any numer? Sorry if im being thick but when a user enters the number and saves, the number is shown as with the comma seperators?

Thank you again,

Steve

Schmoopy
08-13-2009, 03:22 PM
Well that function formats any number yes. I'll list some numbers and their outputs below. The function looks at the number then decides if it needs a comma or not:

300 -> 300
3400 -> 3,400
1 -> 1
29568567 -> 29,568,567

Please be more descriptive if this isn't what you want, but this function works with all numbers, only putting commas when they're necessary.

steveh
08-13-2009, 04:35 PM
sorry if im not explaining myself properly but this is all very new.

So i have entered the code (as per below) but the record, when run does not come back with the number formatted.

[ <td class="style8 style8"><?php echo number_format($row_Recordset1['price'])); ?></td> ]

Im obviously missing something!

Schmoopy
08-13-2009, 05:10 PM
What's it coming out with?

steveh
08-13-2009, 05:47 PM
i fixed it! It was saying "Parse error: parse error, expecting `','' or `';'' etc but it was because i had two brackets after the record!

Now i have removed one of them it works perfectly!

Last question on this, so it works when the data is being viewed but if i was adding or updating a record i would say enter 1000 and when i tabbed to the next box it would stay at a 1000 rather than showing (what i want) 1,000.

The code for the edit form is

<td><input type="text" name="price" value="<?php echo $row_rs_editsub['premium']; ?>" size="32" /></td>

Is there a way that when i tab to the next box the number is updated to 1,000? or am i pushing my luck if it took me this long to do something simple!

Schmoopy
08-13-2009, 06:09 PM
That will require JavaScript to do, nothing to do with PHP there. I'm sure javascript has a similar function to number_format, maybe start a new thread in that section.

Good luck.