Forum Moderators: coopster

Message Too Old, No Replies

generate an invoice number in php

how to...

         

mespejo

5:27 am on Jan 10, 2007 (gmt 0)

10+ Year Member



Hi,

I am new to php and mysql.

Can anyone please show me how to generate a unique invoice number in php and how to set it up in mysql? When the user clicks on the submit button it will take them to a confirmation page where the automatically generated Invoice Number will display.

Thanks.

female designer

5:42 am on Jan 10, 2007 (gmt 0)

10+ Year Member



well, there can be different methods for this depending on your requirements. the most usual one could be to read the company / client name for whom you are preparing the invoice and then read the current time and generate a four digits random number using rand() function and then include the primary key of the invoice from mysql table where you store you invoices or include the client id at the end.

so the finaly random invoice number might be

microsoft_5f55_22 i.e (companyName_fourRandomDigits_customerID)

eelixduppy

5:45 am on Jan 10, 2007 (gmt 0)



I'd have an auto-incrementing [dev.mysql.com] column for unique IDs. If you ever need to get this value for some reason, you could use mysql_insert_id [us3.php.net].

This would make the most sense, and make sure that only unique id's are populated in the database.

Good luck :)

mespejo

5:56 am on Jan 10, 2007 (gmt 0)

10+ Year Member



Using an auto_incrementing column will start from 1. What if I want to use an invoice number that will start from 0000 then 0001, 0002 and so on...

eelixduppy

12:49 pm on Jan 10, 2007 (gmt 0)




To start with an AUTO_INCREMENT value other than 1, you can set that value with CREATE TABLE or ALTER TABLE, like this:

mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;


[dev.mysql.com...]

[edit]
I just realized what you were asking...ooops...I'm not sure how to put leading zeros on an auto increment without manipulating it server-side after you select the results.

whoisgregg

2:58 pm on Jan 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you would just define the auto-incrementing field as `unsigned zerofill`. :)

eelixduppy

3:00 pm on Jan 10, 2007 (gmt 0)



hehe...thanks gregg for saving me there ;)

female designer

6:07 am on Jan 11, 2007 (gmt 0)

10+ Year Member



come on guys, that is piece of cake :)

say you have unique id in $row[pid] that contains '1' then while generating invoice value use,

echo "Your Invoice Number is: 000{$row[pid]}";

but wait if you want to run this series as auto incremented every time a new invoice is generated then you can write an IF statement that will first check the number of digits in the $row[] value and then accordingly put 0s before the value e.g if it has two digits then put two 0s before the value.

mcavic

7:28 am on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can write an IF statement that will first check the number of digits

Yes, you could, or you could just use a MySQL table with a column that's set to zerofill [webmasterworld.com].

whoisgregg

3:09 pm on Jan 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't want to use zerofill in the DB, probably the next most efficient thing is to do is str_pad [php.net] the ID returned from the DB.

$input = 24;
echo str_pad($input, 7, "0", STR_PAD_LEFT); // 0000024

female designer

6:21 am on Jan 12, 2007 (gmt 0)

10+ Year Member



however, i still believe that to use the company name plus customerID will be thest best to generate meaningful invoices, in one of my accounting projects i used

companyName_customerID_date

e.g

Oracle_345_20070106

this helped a lot us in searching invoices and also invoice name gives full information that for whom it belongs to, customerID for a quick client search in DB and date immediately tells you the dispatched date of the invoices.

and these are the most commonly searched three attributes about an invoice and you get this info just by reading the invoice name.