Skip to content

Instantly share code, notes, and snippets.

@helisoncruz
Created March 30, 2015 18:44
Show Gist options
  • Save helisoncruz/ac46858ab0da4ddc5ab1 to your computer and use it in GitHub Desktop.
Save helisoncruz/ac46858ab0da4ddc5ab1 to your computer and use it in GitHub Desktop.
Gerar códigos em seguem cia com function em PHP
function gerar_codigos($tabela,$coluna){
$sel = $this->seleciona("SELECT * FROM ".$tabela." ORDER BY ".$coluna." DESC LIMIT 1");
$contar = mysql_num_rows($sel);
if($contar == '0'){
$codigo = '00001';
}else{
$ln = mysql_fetch_assoc($sel);
$novo_codigo = $ln[$coluna] + 1;
$digitos = strlen($novo_codigo);
switch ($digitos) {
case '1': $zeros = '0000'; break;
case '2': $zeros = '000'; break;
case '3': $zeros = '00'; break;
case '4': $zeros = '0'; break;
case '5': $zeros = ''; break;
}
$codigo = $zeros.$novo_codigo;
}
return $codigo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment