This article focuses on the knowledge that PHP creates a unique numbering class based on the self-increment ID. Has a good reference value. Let's take a look at the little series.
In the development process, our data table generally uses the self-increment number as the ID primary key, but the ID is the numeral type, is not easy to understand. After we have converted the IDs to numbers in a certain format, it is easy to know what is represented by the number.
For example, the order table id=20160111197681234, only look at the ID we do not know that this ID is the ID of the order table, and to the number O-20160111197681234, it is easy to see is the Order table records, You can then search the order form based on the ID.
Rules for numbering creation
1. The only
Generate with a self-increment ID to ensure uniqueness
2. As short as possible
You can use the number to find the letter of the way processing, create a shorter number
Algorithm principle
1. Add a custom prefix to identify
2. The format uses the prefix + the letter + the number composition, the number only retains n bits, exceeds the use number to seek the remainder the way uses the letter correspondence
For example:
Id=1
Prefix =f
Number reserved 3-bit
The number created is:f-a-001
The code is as follows:
IDCode.class.PHP
<?php/** * PHP creates unique numbering class based on self-increment ID * Date: 2016-11-27 * author:fdipzone * Ver: 1.0 * * Func * Public Create number */C Lass idcode{//class start /** * Create number * @param int $id self-increment ID * @param int $num _length digit maximum number * @param string $prefix prefix * @return string * /public static function Create ($id, $num _length, $prefix) { //cardinality $base = Pow (ten, $num _length); Generates a letter part $pision = (int) ($id/$base); $word = "; while ($pision) { $tmp = Fmod ($pision, 26);//Use only 26 uppercase letters $TMP = chr ($tmp + x); Convert to Letter $word. = $tmp; $pision = Floor ($pision/26); } if ($word = = ") { $word = CHR; } Generate a number part $mod = $id% $base; $digital = Str_pad ($mod, $num _length, 0, str_pad_left); $code = sprintf ('%s-%s-%s ', $prefix, $word, $digital); return $code; }} Class end?>
demo.php
<?phprequire ' IDCode.class.php '; $test _ids = Array (1,9,10,99,100,999,1000,1009,2099,3999,9999,14999,99999); foreach ($test _ids as $test _id) { echo $test _id. ' = '. Idcode::create ($test _id, 3, ' F '). ' <br> ';}? >
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 whole content of this article, I hope that everyone's study has helped.