Generate 8 digit sequential number

Asked

Viewed 1,123 times

0

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

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

2 answers

2

To generate a PHP default would be:

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

0


Makes a SELECT to know which code was last inserted:

$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 sets manually even using a if, will subsequently be defined 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
    }
 }
  • Yes, I thought the same way, but what about when the year turns, that you won’t find any code with the year 19? How will I create this x digit code?

  • 1

    I edited the answer

  • 1

    Great friend of mine, that I needed, thank you very much!

  • 1

    Thank you for being able to help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.