Php creates a unique id class based on the auto-increment id

Source: Internet
Author: User
In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.


In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.

For example, order table id = 20160111197681234, only look at the id we do not know this id is the order table id, and converted to the number O-20160111197681234, it is easy to see that is the order table records, then, you can search the order table by id.

Rule created by ID

1. unique
Use auto-incremental id generation to ensure uniqueness

2. as short as possible
You can use numbers to obtain the remainder of the corresponding letter to create a shorter number.

Algorithm principle

1. add a custom prefix for identification

2. the format is composed of a prefix, letters, and numbers. only N digits are retained. if the number exceeds the limit, a letter is used to calculate the remainder.

For example:
Id = 1
Prefix = F
Number reserved 3 bits
The created number is: F-A-001

The code is as follows:

IDCode. class. php

 

Demo. php

 ';}?>

Output:

1 = F-A-0019 = F-A-00910 = F-A-01099 = F-A-099100 = F-A-100999 = F-A-9991000 = F-B-0001009 = F-B-0092099 = F-C-0993999 = F-D-9999999 = F-J-99914999 = F-O-99999999 = F-VD-999



Source code: Click to view

In the development process, we usually use auto-increment numbers as the id primary key for data tables, and id is a numeric type, which is not easy to understand. After converting IDs into numbers in a certain format, it is easy to know what the IDs represent.

For example, order table id = 20160111197681234, only look at the id we do not know this id is the order table id, and converted to the number O-20160111197681234, it is easy to see that is the order table records, then, you can search the order table by id.

Rule created by ID

1. unique
Use auto-incremental id generation to ensure uniqueness

2. as short as possible
You can use numbers to obtain the remainder of the corresponding letter to create a shorter number.

Algorithm principle

1. add a custom prefix for identification

2. the format is composed of a prefix, letters, and numbers. only N digits are retained. if the number exceeds the limit, a letter is used to calculate the remainder.

For example:
Id = 1
Prefix = F
Number reserved 3 bits
The created number is: F-A-001

The code is as follows:

IDCode. class. php

 

Demo. php

 ';}?>

Output:

1 = F-A-0019 = F-A-00910 = F-A-01099 = F-A-099100 = F-A-100999 = F-A-9991000 = F-B-0001009 = F-B-0092099 = F-C-0993999 = F-D-9999999 = F-J-99914999 = F-O-99999999 = F-VD-999


The above is the content of the unique id class created by php based on the auto-increment id. For more information, see PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.