Creating an Identification Code with PHP and Mysql

Asked

Viewed 45 times

-1

I am starting to work with programming now and I have a lot of questions. I need to create a specific "TAG" for equipment registration. This TAG must have 4 letters + 4 digits, ex: CEME-0001. The first 04 letters refer to the sector where the equipment is located and the 4 numbers a value to be increased with each new registration. The letters of the sectors will come from the table "tb_sectors" of the field "sigla_sector". Any idea how I can make a code to perform this action when registering an equipment? I will also need to update this field, being possible to transfer the sector equipment, which in case will change the letters and increase the numbers according to the amount of equipment of the transferred sector.

  • Post the structure of your database, you probably won’t even need to do anything else, depending on how you are, you keep this data (acronym-number) in different columns and when you search or show the user just join

  • Welcome Ronaldo Souza, here is a post to get you successful in your next questions. How to create a minimal example https://answall.com/help/mcve. It is also important to mark how you accept a response that has solved your problem. Here’s how to mark a response as it accepts https://i.stack.Imgur.com/evLUR.png and here’s why to accept a response

1 answer

1

As a welcome toast will an answer, in your next questions follow the post I recommended in the comment of your question

//conexão ao banco
$conn = new mysqli("localhost", "USUARIO", "SENHA", "nome_DB");

//Letras dos setores recuperadas via post do formulário
$centro=$_POST["centro"];

/*seleciona um registro (limit 1) cujo as 4 primeiras letras são iguais
a variável acima na ordem descendente da coluna tag, que significa dizer, o maior, 
pois a parte das letras com tracinho são iguais e o que vai prevalecar na ordenação é
a parte numerica.******/    

$query = ("SELECT tag FROM cadastro WHERE LEFT(tag,4)='$centro' order by tag DESC limit 1");

$result = mysqli_query($conn,$query);

    while($row = mysqli_fetch_assoc($result)) {
         //o registro
         $tag = $row["tag"];
         //separamos a parte numerica
         $num=substr($tag,5);
    } 

    /*****caso haja registro insere incrementando, caso contrário
    vai pro else inserir o primeiro******/
    if ($num!=""){
        //somamos 1 a parte numerica
        $proximo=($num+1);
        /**retorna o comprimento da variável acima, necessário pq o PHP ao somar 0001 + 1
        retorna 2, 0012 + 1 retorna 13, 0155 + 1 retorna 156  etc... dai 
        precisamos saber quantos zeros a esquerda serão necessários***/
        $comprimento=strlen($proximo);
        //quantidade e zeros necessários
        $zerosEsquerda = "4-$comprimento";
        // função responsável por colocar os zeros à esquerda
        $num = str_pad($proximo, $zerosEsquerda, '0', STR_PAD_LEFT);
        // preparação do value da declaração insert
        $strProximo= $centro."-".$num;

        $conn->query("Insert into cadastro (tag) values ('".$strProximo."')");

    }else{

        $primeiro=$centro."-0001";
        $conn->query("Insert into cadastro (tag) values ('".$primeiro."')");

    }

Browser other questions tagged

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