Jump to content

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

Edited by requinix

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. :)

I suppose there's no reason. Personally I'd like transaction ID's to be more random/unique than the current timestamp + the current auto_increment ID. Using the current date/timestamp for instance limits to numbers only. :)

Edited by Mikey
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.