By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Formatting intergers in php

P: 274
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.
Oct 27 '08 #1
Share this Question
Share on Google+
2 Replies

Expert 5K+
P: 5,058
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:
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `tbl`( 
  2.   ID Int(5) Zerofill
  3. );
Populated with the numbers 1, 12 and 123 would return the following data:
Expand|Select|Wrap|Line Numbers
  1. 00001
  2. 00012
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. echo str_pad(1, 5, "0", STR_PAD_LEFT);
Which should output: 00001
Oct 27 '08 #2

P: 274
Thanks a lot,
It helped,
Oct 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.