简体   繁体   中英

How to generate a custom ID in CodeIgniter?

$cc = $this->db->count_all('job_card');
$coun = str_pad($cc,4,STR_PAD_LEFT);
$id = "JI"."-";
$d = date('y') ;
$mnth = date("m"); 
$customid = $id.$d.$mnth.$coun;

I am generating and ID of "JI-18051000", but I need it to generate "JI-18050001".

You're missing the third parameter of str_pad() which indicates what should be used for the padding.

In this case, you want to add 0 , so you need to add that to your code:

$cc = $this->db->count_all('job_card');
$coun = str_pad($cc, 4, 0, STR_PAD_LEFT); // Updated line to include '0'
$id = "JI"."-";
$d = date('y') ;
$mnth = date("m");
$customid = $id.$d.$mnth.$coun;
use the following line:
$coun = str_pad($cc,4,0,STR_PAD_LEFT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM