开发者

How to generate 5 digit alphanumeric unique ID using codeigniter?

开发者 https://www.devze.com 2023-02-01 06:21 出处:网络
I have a project in which I need to generate a unique 5 digit alphanum开发者_如何学运维eric ID for the user. How can I achieve this using codeigniter!?

I have a project in which I need to generate a unique 5 digit alphanum开发者_如何学运维eric ID for the user. How can I achieve this using codeigniter!?

thanks


There is a function for that in string helper called random_string.

$this->load->helper('string');
echo random_string('alnum',5);

Details


You could try using uniqid()


Since you need a unique id, then you should use a unique data from the user record (index for example) or just generate a random one and directly check it's occurrence in the DB.

Here are the two approaches:

  1. random strings
  2. Unique IDs


Convert the user's numeric id (which I am assuming you already have) to base-36 using base_convert($id, 10, 36);


The below piece of code has to be written under calculation field in sharepoint list to generate combination alpha numeric item id dynamically in the incremental model.

=field1&"-"&LEFT("00000",5-(LEN(TEXT([field2],"0"))))&TEXT([field2],"0")

filed1=contains the prefix alpha value i.e (xyz,abc)

field2=contains the starting value of the field1 range i.e (1,10,20)

output: xyz-00001,abc-00010..


I would highly recommend to use UUID for such purposes, as you project definitely will grow into something more serious.

For example, here's how we do in our project UID generation:

<?php 
// ...
public function getUid($uid = null)
{
    while (! $this->isValid($uid)) {
        $uid = sprintf('%04x%04x%02x', mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xff));
    }
    return $uid;
}

or find out another thread, with more complicated algorithms of generating UUID v4

0

精彩评论

暂无评论...
验证码 换一张
取 消