Fixed letters in a mysql field using php

0

I have this code that generates a consecutive number every time I insert a data and I would like to add the fixed ESP letters so that it comes out every time I add a new record, the code is as follows:

 $codigo = (empty($consulta['num']) ? 1 : $consulta['num']+=1);
 echo 'El codigo actual es: '.$codigo;

 $consulta = mysqli_query($mysql,'INSERT INTO num_ficha (num) VALUES  ('.$codigo.')');
 if(!$consulta){die('Error');}
    
asked by Alberto Gigorro 18.04.2018 в 12:23
source

1 answer

0

Then the only thing you need is to add the text of ESP in front of the code:

$codigo = (empty($consulta['num']) ? 1 : $consulta['num']+=1); echo 'El codigo actual es: '.$codigo;

$codigo='ESP'.$codigo; // te faltaria añadir esto.

$consulta = mysqli_query($mysql,'INSERT INTO num_ficha (num) VALUES ('.$codigo.')'); if(!$consulta){die('Error');}

However, I suggest that, when you are going to present the data (sure that at some point you do the SELECT query), at that moment add the 'ESP' in front.

This suggestion comes because most likely that code is from SPAIN, but when you have to internationalize it, it will be more convenient to relate the file with language / country, and then show the code according to that country. If this chip has to be changed and you have it inserted in the table, you would have to do truncate operations on that code.

In summary, you better do something kind:

$consulta = mysqli_query($mysql,'SELECT * FROM num_ficha'); 
if(!$consulta){die('Error');}

foreach($consulta as $fila) {
    echo 'ESP'.$fila->get('num');
}
    
answered by 18.04.2018 в 12:26