php 根据自增id创建唯一编号类

黄舟
黄舟 原创
2023-03-05 19:16:02 935浏览


在开发过程中,我们数据表一般都使用自增数字作为id主键,而id是数字型,不容易理解。我们把id按一定格式转为编号后,很容易根据编号知道代表的是什么内容。

例如订单表id=20160111197681234,只看id我们并不知道这个id是订单表的id,而转为编号O-20160111197681234,则很容易看出是订单表的记录,然后可以根据id在订单表中搜寻。

编号创建的规则

1.唯一
使用自增id生成,保证唯一性

2.尽可能短
可使用数字求余对应字母的方式处理,创建较短的编号

算法原理

1.加自定义前缀,用于标识

2.格式使用前缀+字母+数字组成,数字只保留N位,超过的使用数字求余的方式使用字母对应

例如:
id=1
前缀=F
数字保留3位
则创建的编号为:F-A-001

代码如下:

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

<?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>';
}?>

输出:

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



源码下载地址:点击查看

在开发过程中,我们数据表一般都使用自增数字作为id主键,而id是数字型,不容易理解。我们把id按一定格式转为编号后,很容易根据编号知道代表的是什么内容。

例如订单表id=20160111197681234,只看id我们并不知道这个id是订单表的id,而转为编号O-20160111197681234,则很容易看出是订单表的记录,然后可以根据id在订单表中搜寻。

编号创建的规则

1.唯一
使用自增id生成,保证唯一性

2.尽可能短
可使用数字求余对应字母的方式处理,创建较短的编号

算法原理

1.加自定义前缀,用于标识

2.格式使用前缀+字母+数字组成,数字只保留N位,超过的使用数字求余的方式使用字母对应

例如:
id=1
前缀=F
数字保留3位
则创建的编号为:F-A-001

代码如下:

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

<?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>';
}?>

输出:

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


以上就是php 根据自增id创建唯一编号类的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。