mukeshprasad4u Posted February 20, 2013 Share Posted February 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/ Share on other sites More sharing options...
mweldan Posted February 20, 2013 Share Posted February 20, 2013 I would keep field `id` for running number and use another field `whatever_id` to refer whatever . that way each record would stay unique and i can use any way i want to refer that row. Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/#findComment-1413546 Share on other sites More sharing options...
requinix Posted February 20, 2013 Share Posted February 20, 2013 (edited) 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 February 20, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/#findComment-1413547 Share on other sites More sharing options...
Mikey Posted February 20, 2013 Share Posted February 20, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/#findComment-1413551 Share on other sites More sharing options...
Jessica Posted February 20, 2013 Share Posted February 20, 2013 Why go to that trouble when you can use an auto incrementing field? Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/#findComment-1413552 Share on other sites More sharing options...
Mikey Posted February 20, 2013 Share Posted February 20, 2013 (edited) 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 February 20, 2013 by Mikey Quote Link to comment https://forums.phpfreaks.com/topic/274709-best-way-to-generate-transaction-id-for-a-point-of-sale-application/#findComment-1413555 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.