


This article mainly introduces the relevant knowledge of PHP to create a unique number class based on the self-increasing ID. Has very good reference value. Let’s take a look with the editor below
During the development process, our data tables generally use self-increasing numbers as the primary key of id, and id is a numeric type, which is not easy to understand. After we convert the ID into a number according to a certain format, it is easy to know what the content represents based on the number.
For example, the order table id=20160111197681234, just looking at the id we don’t know that this id is the id of the order table, but it is converted to the number O-20160111197681234, it is easy to see that it is a record of the order table, and then it can be searched in the order table based on the id.
Number creation rules
1. The unique
is generated using an auto-incremented id, guaranteed Uniqueness
2. As short as possible
can be processed by using the remainder of the number and the corresponding letter to create a shorter number
Algorithm principle
1. Add a custom prefix to identify
2. The format is composed of prefix + letters + numbers. Only N digits are reserved for numbers. If there are more than 10 digits, use the remainder method of numbers and use letters to correspond.
For example:
id =1
Prefix=F
The number retains 3 digits
The number created is: F-A-001
The code is as follows:
IDCode.class.PHP
##
<?php /** * php 根据自增id创建唯一编号类 * Date: 2016-11-27 * Author: fdipzone * Ver: 1.0 * * Func * Public create 创建编号 */ class IDCode{ // class start /** * 创建编号 * @param Int $id 自增id * @param Int $num_length 数字最大位数 * @param String $prefix 前缀 * @return String */ public static function create($id, $num_length, $prefix){ // 基数 $base = pow(10, $num_length); // 生成字母部分 $pision = (int)($id/$base); $word = ''; while($pision){ $tmp = fmod($pision, 26); // 只使用26个大写字母 $tmp = chr($tmp + 65); // 转为字母 $word .= $tmp; $pision = floor($pision/26); } if($word==''){ $word = chr(65); } // 生成数字部分 $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
<?php require '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-001 9 = F-A-009 10 = F-A-010 99 = F-A-099 100 = F-A-100 999 = F-A-999 1000 = F-B-000 1009 = F-B-009 2099 = F-C-099 3999 = F-D-999 9999 = F-J-999 14999 = F-O-999 99999 = F-VD-999
The above is the entire content of this article, I hope it will be helpful to everyone's study.
PHPHow to locate the cause of the fault through strace
PHPUsing file_get_contents to send http requests is simple
PHP MySQL high concurrency locking transaction processing Problem Solution
The above is the detailed content of PHP method to create a unique number class based on an auto-incremented ID. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
