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

invoice number

P: 10
hi all,

i am new to php.how to generate invoice number (EX:INV2008001) this format.

plz reply me the code....


thanks
palani
Feb 20 '08 #1
Share this Question
Share on Google+
21 Replies


MarkoKlacar
Expert 100+
P: 296
Hi,

I can't spoonfeed you the code, but you'll want to have a look at rand as a start.

Should be pretty straight forward for you.
Feb 20 '08 #2

Markus
Expert 5K+
P: 6,050
Hi,

I can't spoonfeed you the code, but you'll want to have a look at rand as a start.

Should be pretty straight forward for you.
That could produce a duplicate invoice number, though.

I think the best way would be to have an invoice number : INV20000001, and then increment that.

Save it in a db maybe.
Feb 20 '08 #3

hsriat
Expert 100+
P: 1,654
That could produce a duplicate invoice number, though.

I think the best way would be to have an invoice number : INV20000001, and then increment that.

Save it in a db maybe.
Or use this...
[php]
$new_invoice = gettimeofday();
$new_invoice = $new_invoice[sec];
[/php]

This will help if the invoice is generated not too fast. (Not more than once in a sceond)
Feb 20 '08 #4

MarkoKlacar
Expert 100+
P: 296
That's true, as you say you should have a combination.

My post was a bit clumsy, thanks for pointing it out.
Feb 20 '08 #5

Markus
Expert 5K+
P: 6,050
Or use this...
[php]
$new_invoice = gettimeofday();
$new_invoice = $new_invoice[sec];
[/php]

This will help if the invoice is generated not too fast. (Not more than once in a sceond)
But still, it's not relevant to the invoices..

You'd typically (or is it just me) want to be able to keep track of the amount of invoices sent.. and don't invoice numbers generally go up as they are a way of tracking someone?
Feb 20 '08 #6

hsriat
Expert 100+
P: 1,654
But still, it's not relevant to the invoices..

You'd typically (or is it just me) want to be able to keep track of the amount of invoices sent.. and don't invoice numbers generally go up as they are a way of tracking someone?
Yeah, that will only help if you have to keep them unique. Not a counting of all.
But with this there is no db query required.

Or one other option can be...
- make a file with 0 written inside it.
- every time you create a new invoice, read that file and create the invoice like : [php]
//$file_content = content of the saved file
$invoice = "INV";
for ($i = 0; $i<10 - strlen($file_content); i++)
$invoice .= "0";
$invoice .= $file_content;

$file_content++;
//then save $file_content to the same file.
[/php]
Feb 20 '08 #7

Markus
Expert 5K+
P: 6,050
I catch dig your jammin!

But, ultimately, it is better to use a database to keep track of records.

Look at us, i bet the OP has forgotten about this thread..
Feb 20 '08 #8

hsriat
Expert 100+
P: 1,654
I catch dig your jammin!

But, ultimately, it is better to use a database to keep track of records.

Look at us, i bet the OP has forgotten about this thread..
Yeh you are right in both the things...

1. Its better to use db, coz you might need to keep some other info about the invoice.. eg.. date, time, customer etc.

2. OP has forgotten about the thread.
Feb 20 '08 #9

TheServant
Expert 100+
P: 1,168
Good discussion though. How about using database table attributes:
INT(5) ZEROFILL AUTO_INCREMENT ? That way everytime a row is added it will increase by one but will appear as 00001 then 00002 etc...

Good idea/bad idea? I ask cos this is how I plan to set my system up.
Feb 20 '08 #10

ronverdonk
Expert 2.5K+
P: 4,258
Good discussion though. How about using database table attributes:
INT(5) ZEROFILL AUTO_INCREMENT ? That way everytime a row is added it will increase by one but will appear as 00001 then 00002 etc...

Good idea/bad idea? I ask cos this is how I plan to set my system up.
Nothing wrong with the idea in itself, BUT ... never reorganize that table because your run the risk that it will be renumbered (assuming you sometimes delete rows from the table).

Ronald
Feb 20 '08 #11

TheServant
Expert 100+
P: 1,168
Nothing wrong with the idea in itself, BUT ... never reorganize that table because your run the risk that it will be renumbered (assuming you sometimes delete rows from the table).

Ronald
OK a little confused by what you mean. If i make a table:
00001 John
00002 Zed
00003 Adam

Then delete Row 2 (Zed). The next time I INSERT a row it will become:
00001 John
00003 Adam
00004 Tom

No repeat?

If I order by alphabetical order:
00003 Adam
00001 John
00004 Tom

Then INSERT a new row, I think it will be:
00003 Adam
00001 John
00004 Tom
00005 Wayne

Let me know if I am wrong, and also let me know if this is not what you meant. Thanks for your input!
Feb 21 '08 #12

Markus
Expert 5K+
P: 6,050
Good discussion though. How about using database table attributes:
INT(5) ZEROFILL AUTO_INCREMENT ? That way everytime a row is added it will increase by one but will appear as 00001 then 00002 etc...

Good idea/bad idea? I ask cos this is how I plan to set my system up.
I like, and i didn't know about the ZEROFILL!
So it shall now be with me on my travels!
Feb 21 '08 #13

TheServant
Expert 100+
P: 1,168
I like, and i didn't know about the ZEROFILL!
So it shall now be with me on my travels!
w00t! I actually knew something someone didn't know! No offense markusn00b, u have already taught me so much, I'm glad to contribute something useful!
Feb 21 '08 #14

Markus
Expert 5K+
P: 6,050
w00t! I actually knew something someone didn't know! No offense markusn00b, u have already taught me so much, I'm glad to contribute something useful!
Well, well!

Student has become the teacher!
Feb 21 '08 #15

TheServant
Expert 100+
P: 1,168
Anyway, Ronald, how about my question above?
Feb 21 '08 #16

ronverdonk
Expert 2.5K+
P: 4,258
Anyway, Ronald, how about my question above?
Sorry, I have been away for a day.
What I meant is that when you REORGANIZE your table all the auto_increment values are also reorganized. So your table
Expand|Select|Wrap|Line Numbers
  1. 00001 John
  2. 00003 Adam
  3. 00004 Tom
  4. 00005 Wayne
wll after the reorganize look:
Expand|Select|Wrap|Line Numbers
  1. 00001 John
  2. 00002 Adam
  3. 00003 Tom
  4. 00004 Wayne
But of course, if you never reorganize, that's fine. And if your auto_increment numbers do not represent values like an invoice number, fine. My remark was just a liitle warning.

Ronald
Feb 21 '08 #17

TheServant
Expert 100+
P: 1,168
Sorry, I have been away for a day.
What I meant is that when you REORGANIZE your table all the auto_increment values are also reorganized. So your table
Expand|Select|Wrap|Line Numbers
  1. 00001 John
  2. 00003 Adam
  3. 00004 Tom
  4. 00005 Wayne
wll after the reorganize look:
Expand|Select|Wrap|Line Numbers
  1. 00001 John
  2. 00002 Adam
  3. 00003 Tom
  4. 00004 Wayne
But of course, if you never reorganize, that's fine. And if your auto_increment numbers do not represent values like an invoice number, fine. My remark was just a liitle warning.

Ronald
Fair enough. I think I am thinking of something different. When you say reorganise, do you mean ORDERBY or is there a REORGANISE function?
Feb 21 '08 #18

ronverdonk
Expert 2.5K+
P: 4,258
Fair enough. I think I am thinking of something different. When you say reorganise, do you mean ORDERBY or is there a REORGANISE function?
The original question in this thread was about invoices.
By reorganize I mean just a SELECT INSERT sequence wehereby you get rid of the holes in your table sequences. MySQL does not have a REORGANIZE function (yet).

Ronald
Feb 21 '08 #19

TheServant
Expert 100+
P: 1,168
Sorry to go off topic a bit, but I think the OP has left:
If I have a hole and go INSERT, it shouldn't plug up the hole but continue the sequence. I have tried this before, so I guess I want to know how to plug that hole so I can avoid doing it!
Feb 22 '08 #20

ronverdonk
Expert 2.5K+
P: 4,258
Sorry to go off topic a bit, but I think the OP has left:
If I have a hole and go INSERT, it shouldn't plug up the hole but continue the sequence. I have tried this before, so I guess I want to know how to plug that hole so I can avoid doing it!
This goes beyond the thread problem. I will ans wer this and then close this discussion. I was not talking about a simple INSERT, but re-arranging the auto_increment index into a new table using INSERT SELECT...

People do that because they have a very heavily changing table and are afraid that the auto_increment index is running out. (an INT as key and a million changes per day can get you into such a situation).


Ronald
Feb 22 '08 #21

Markus
Expert 5K+
P: 6,050
Forgot to close it ;)
Feb 22 '08 #22

Post your reply

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