Results 1 to 8 of 8

Thread: formatting a record to a number - newbie!

  1. #1
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default formatting a record to a number - newbie!

    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

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Here's a function that should do the trick:

    PHP Code:
    <?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.

  3. #3
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.

  5. #5
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    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!

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    What's it coming out with?

  7. #7
    Join Date
    Mar 2005
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile

    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!

  8. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    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.

  9. The Following User Says Thank You to Schmoopy For This Useful Post:

    steveh (08-13-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •