Record sequential number per year PHP

Asked

Viewed 548 times

0

I wanted to create an automatic numbering record but reset each year. Getting for example 170001...170002 and for the year 180001...180002

But with this code, I always register 1700001

<?php 
   function conectarBanco(){
      return new mysqli('localhost', 'root', '', 'bvmarco');
   }
   function primeiraOrdemAno(){
      return date('y'). '00001';
   }
   function novaOrdemServico(){
      $db = conectarBanco();
      $sql = 'SELECT max(Id) as `ultimo_id` FROM `participacao` WHERE year(data) = year(now()) ';

   $result = $db->query($sql);

   if($result === true){
      $ordem_servico = $result->fetch_assoc();
      return ++$ordem_servico['ultimo_id'];
   }else{
      return primeiraOrdemAno();
   }
   }
   $nova_ordem = novaOrdemServico();
?>

<input type="hidden" name="id" class="form-control" value="<?php echo $nova_ordem ?>" required>

With help I managed to make the code. When you need a tip.

<?php
   function conectarBanco(){
      return new mysqli('localhost', 'root', '', 'bvmarco');
   }
   function primeiraOrdemAno(){
      return date('y'). '00001';
   }
   function novaOrdemServico(){
      $db = conectarBanco();
      $sql = 'SELECT max(Id) as `ultimo_id` FROM `participacao` WHERE year(data) = year(now()) ';
      $result = $db->query($sql);
      if($result === true){
         $ordem_servico = $result->fetch_assoc();
         return ++$ordem_servico['ultimo_id'];
      }else{
         return primeiraOrdemAno();
      }
   }
   $nova_ordem = novaOrdemServico();
?>
  • 170001 is for 2017 and 180001 is for 2018?

  • 1

    And if you get to 179999 before you turn the year, what happens?

  • Correct, 17 at the beginning refers to 2017 and 18 refers to 2018

  • It would help if you could [Dit] the question pointing out the difficulty found.

  • wanted it to increase to 1710000

  • The record is always the same

  • If the question is true, is there any way to describe why you need a value like this? Please answer this by editing the question, not in the comments. Also include the details given in the other comments. Any and all information of the question should be in the question.

Show 2 more comments

1 answer

2


In pure SQL you can get the next serial so:

SELECT
   CONCAT(
      YEAR(CURRENT_DATE) % 100,
      LPAD( COALESCE( MAX(serial_do_ano), 0 ) + 1, 4, '0' )
   ) AS serial
FROM
   tabela
WHERE
   YEAR(CURRENT_DATE) = YEAR(data)

Probably it should be unnecessary to say, but you need to enter the ID in the bank, so that the next one is incremented, obviously.


Points of interest:

  • CONCAT: unites several values in one string only;
  • LPAD: complements the zeros to the left of the id;
  • COALESCE: chooses a value default if the ìd (the first of the year).

In case we use 4 in the LPAD. If you want the number of boxes not to be limited, you can adapt with a IF, sort of like this:

IF( valor > 9999, valor, LPAD( valor, 4, '0' ) )
  • But I wanted to produce a sequential algorithm

  • That’s exactly what the above code does. Of course you need to enter the new ID in the bank, otherwise it will be stopped right... It always generates a new ID based on the previous one.

  • the outgoing result is 171700

  • @Everson is grateful for the intention, but with autoincrement you can’t do what the author wants. You have to enter the previous ID together, otherwise it will be a problem at the turn of the year (it will not return to end 0001), so I removed your fiddle. Then, if I have a little free time, I complement with a functional example and explain better. Anyway, thank you again..

  • where to insert the code{IF( value > 9999, value, LPAD( value, 4, '0' ) )}

  • @Adelinovasconcelos recommend not even use IF, it was just an example if you pass 9999. What you can do is the following, increase the number of houses with zero (change 4 by 5 or more) so your number is with equal number of digits.

  • Ah, and on the 171700, when storing, do not put the 17 of the year in the DB, only the 0001 0002 etc. - The ideal in your case is even not to store the year next to the serial, it would be better to keep in the table only the date + 0001, date + 0002 and catch the "17"." 0001" at select time or in the application itself. It becomes simpler, takes up less space. To protect your application from invalid inserts and speed up the search, you can make an INDEX by taking YEAR + SERIAL

Show 2 more comments

Browser other questions tagged

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