Generate 8-digit sequential number

0

I need to generate an 8-digit sequential number following the bank ..

I have the code column in the budget table, it has the value of ex: 1800000001 where 18 is the year and the rest is a sequential number, I need to check the system for the last budget and create a next sequential number in the system. example is 1800000001, I want it to generate the code 1800000002 and so on, as long as when the last number reaches two digits it replaces a 0, when 3, two zeros, eg: 1800000099, this number after the year has to contain 8 houses. / p>     

asked by anonymous 03.05.2018 / 16:11

2 answers

0

Make a SELECT to know which code was last entered:

$sql="SELECT codigo FROM sua_tabela ORDER BY codigo DESC LIMIT 1";

$ultimo_resultado='1800000001';
$ultimo_resultado=intval($ultimo_resultado)+1;
//1800000002

The first number, you can check in the system if there is any record, if there is no vc manually set even using a if , they will subsequently be set automatically.

$ano=date('y');//pega o ano corrente no formato 19, como no seu código
if(!resultados){//verifica se não existem resultados do sql
    $primeiro_codigo=$ano.00000001;//cria o primeiro, no caso 18
}else{//se tiver resultados
    $ano_atual=substr($ultimo_codigo,0,2);//pega o ano atual do ultimo código
    if($ano_atual != $ano){//compara com o ano corrente
        $primeiro_codigo=$ano.00000001;//se for diferente cria o código novo, quando passar para 2019
    }
 }
    
03.05.2018 / 16:53
2

To generate a pattern in PHP would be:

$ultimoNro = '1800000001';
$ultimoNro = substr($ultimoNro, 2);
$proximoNro = $ultimoNro + 1;
$proximoNro = date('y').str_pad($proximoNro, 8, '0', STR_PAD_LEFT);
echo $proximoNro;
    
03.05.2018 / 17:00