Created
March 30, 2015 18:44
-
-
Save helisoncruz/ac46858ab0da4ddc5ab1 to your computer and use it in GitHub Desktop.
Gerar códigos em seguem cia com function em PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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