Formatting intergers in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creative1
    Contributor
    • Sep 2007
    • 274

    Formatting intergers in php

    hi,
    I have setup a query to insert datab in the databse and then display the ID(an auto increment primary key) of the record on the screen. ID type is bigint and it works fine all the way I want it. My question is I want to display like this
    Assigned ID: QUT-0000000001
    where as appears as follows:
    Assigned ID: QUT-1
    What I can do to do that?
    1 is the id that I got from database. Sounds like it is easy to do it but I can't find any example. I do see some for fractional values.
    Thanks in advance.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    If this database is using MySQL, then you could simply add the ZEROFILL parameter to your column. That would automatically pad the value with zeros.

    For example, this table:
    [code=mysql]
    CREATE TABLE `tbl`(
    ID Int(5) Zerofill
    );
    [/code]
    Populated with the numbers 1, 12 and 123 would return the following data:
    Code:
    00001
    00012
    00123
    If you are not using MySQL, or would rather do this in PHP, you could use the str_pad function in PHP.
    Like, for example:
    [code=php]
    echo str_pad(1, 5, "0", STR_PAD_LEFT);
    [/code]
    Which should output: 00001

    Comment

    • creative1
      Contributor
      • Sep 2007
      • 274

      #3
      Thanks a lot,
      It helped,
      Regards,
      C

      Comment

      Working...