Jump to content

Best way to generate transaction id for a point of sale application


mukeshprasad4u

Recommended Posts

Hi ,

I am new to the php.I am building an php pos application to learn php by doing.

I want to generate a unique transaction id whenever any transactions like purchase order,purchase return,sales or sales return etc.

But confused how to accomplise this.Is uniquid feature of php is sufficeint for this.

I wanted to generate it in format like PO20130220001 ie. PO+date+3 or 4 digit number.

 

Any kind of suggestion is of great help to me.

Are you using a database for this? MySQL? Know about auto_increment?

 

[edit] Eh, I'll elaborate.

If you create a primary key on a table consisting of a field for the date and secondary field that's an auto_increment then MySQL will handle the uniqueness for you.

 

INSERT INTO table (date, ...) VALUES ("20130220", ...)

date     | increment | ...
---------+-----------+----
20130220 |         1 | ...

INSERT INTO table (date, ...) VALUES ("20130220", ...)

date     | increment | ...
---------+-----------+----
20130220 |         1 | ...
20130220 |         2 | ...

The ID is then "PO" + date + increment formatted to however many digits you want. Such as

echo sprintf("PO%s%03d", "20130220", "1"); // PO20130220001

uniqid isn't usually random enough, for example it may (extremely slim chance here) generate the same integer twice. Though you may be able to use uniqid in conjunction with a MySQL field which has a unique index set on it.

 

Personally I'd make a php function which generates a random string or a pre-determined length, (for example, PO9028341), and then check the database within that function to see if that string already exists as a purchase ID. If it does, recursion, if it doesn't, return. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.