Database tables usually have an auto-incrementing primary key with a field type of int and named id.
Advantages: Using int type can auto-increment and saves storage space than character type.
Disadvantages: id is a numeric type. When you want to query the data corresponding to an id, it is difficult to determine which data table it is based on the id.
Therefore, we should use int when storing, and perform some processing on the id when displaying, so that it is easy to tell which table this id belongs to.
Written the following class to add prefixes to ids and restore prefixed ids.
Prefix.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
demo.php
1 |
|
Output:
1 2 3 4 5 6 7 |
|
Customized prefix constants can be created according to needs.
This article explains how to use php to perform ID prefix formatting. For more related content, please pay attention to the php Chinese website.
Related recommendations:
How to print_r through php Method to restore the processed data to the original array
##Determine the connection through PDO in php Available methods
The above is the detailed content of How to perform ID prefix formatting class through php. For more information, please follow other related articles on the PHP Chinese website!