Recommend this page to a friend! |
All requests | > | sequential roll number | > | Request new recommendation | > | Featured requests | > | No recommendations |
by muhammed salim k - 6 years ago (2018-02-16)
0 | How do I generate sequential numbers with characters in PHP like “16it” default number and increment number is 01,02 like that(16it01)? |
1. by zinsou A.A.E.Moïse - 6 years ago (2018-02-20) Reply
hi the goal you try to achieve is really simple.if i take your example, 16it as base and then increment like 01,02 etc... we need here to know the max length for your sequential number to do it and the real range eg: 16it01-16it09
you can try this yourself and alter it to fit your requirement
function sequential_id($max=6,$data='sequential_id.txt',$prepend='16it'){
$i=0;
$maxnumber='';
while($i<($max-strlen($prepend))){
$maxnumber .='9';
$i++;
}
if(file_exists($data)){
$id=trim(file_get_contents($data));
if(strlen($id)==strlen($maxnumber)){
/*you must use only one file per counter otherwise the file will be formatted
to restart counter according to $max arg */
if($id==$maxnumber) return false;/*if the max number with the length $max
is reached the response will always be false; so be sure to test the value before use it*/
$id++;
file_put_contents($data,($id=str_pad($id,strlen($maxnumber),'0',STR_PAD_LEFT)));
return $prepend.$id;
}else{
$id=0;
file_put_contents($data,($id=str_pad($id,strlen($maxnumber),'0',STR_PAD_LEFT)));
return $prepend.$id;
}
}else{
$id=0;
file_put_contents($data,($id=str_pad($id,strlen($maxnumber),'0',STR_PAD_LEFT)));
return $prepend.$id;
}
}
Recommend package | |
|