Generate single random value with php

1

I need to generate a random value with letters and numbers in PHP, always starting with the letter A and having numbers and letters mixed with the value. It is referring to the "control code" field contained in this script:

    <?php require("header.php");?>
<?php require("menu.php");

if(isset($_POST['bt_save_df']))
{
$nome = $_POST['nome'];
$arquivo = $_POST['arquivo'];
$codigo = $_POST['codigo'];
$codcontrol = $_POST['codcontrol'];
$tipo = $_POST['tipo'];
$material = $_POST['material'];
$denominacao = $_POST['denominacao'];

$uploaddir = 'desenhos/arquivos/';
$pastadestino = 'desenhos/arquivos/';
// Pega a extensão do arquivo
$dividir2 = end(explode(".", $_FILES['imagem1']['name']));

// Gera um nome aleatório
$nome = time();
$nome_imagem2 = $nome . '1.' . $dividir2;
$uploadfile = $uploaddir . $nome . '1.' . $dividir2;
if (move_uploaded_file($_FILES['imagem1']['tmp_name'], $uploaddir . $nome_imagem2)) {

}

$arquivo = $pastadestino.$nome_imagem2;


$sql = 'INSERT INTO tbl_desenho (nome, arquivo, codigo, codcontrol, tipo, material, denominacao)';
$sql .= ' VALUES (:nome, :arquivo, :codigo, :codcontrol, :tipo, :material, :denominacao)';

try{

    $create = $db->prepare($sql);
    $create->bindValue(':nome', $nome, PDO::PARAM_STR);
    $create->bindValue(':arquivo', $arquivo, PDO::PARAM_STR);
    $create->bindValue(':codigo', $codigo, PDO::PARAM_STR);
    $create->bindValue(':codcontrol', $codcontrol, PDO::PARAM_STR);
    $create->bindValue(':tipo', $tipo, PDO::PARAM_STR);
    $create->bindValue(':material', $material, PDO::PARAM_STR);
    $create->bindValue(':denominacao', $denominacao, PDO::PARAM_STR);

    if($create->execute() ){
        echo '<script type="text/javascript" >
                alert( "Registro Cadastrado com Sucesso!"); location.href="ger_arquivos.php";
        </script>';

    }


} catch (PDOException $e) {
        echo "Erro ao Cadastrar Registro! - " . $e->getMessage();
    }
}

?>


            <!-- BEGIN Main Content -->
            <div class="row-fluid">
                <div class="span12">
                    <div class="box">
                        <div class="box-title">
                            <h3><i class="icon-reorder"></i> Cadastro de Desenhos</h3>
                            <div class="box-tool">
                                <a data-action="collapse" href="#"><i class="icon-chevron-up"></i></a>
                                <a data-action="close" href="#"><i class="icon-remove"></i></a>
                            </div>
                        </div>
                        <div class="box-content">
                            <form action="cad_arquivo.php" class="form-horizontal" id="validation-form" method="post" enctype="multipart/form-data">   
                                <div class="control-group">
                                    <label class="control-label" for="nome">Titulo:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <input type="text" name="nome" id="nome" class="input-xlarge" />
                                        </div>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="titulo">Código:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <input type="text" name="codigo" id="codigo" class="input-xlarge" />
                                        </div>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="titulo">Código de Controle:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <input type="text" name="codcontrol" id="codcontrol" class="input-xlarge" />
                                        </div>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="select">Tipo:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <select name="tipo" id="tipo">
                                                <option value=""> -- Selecione --</option>
                                                <option value="1">Produção</option>
                                                <option value="2">Kits</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="titulo">Material:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <input type="text" name="material" id="material" class="input-xlarge" />
                                        </div>
                                    </div>
                                </div>

                                <div class="control-group">
                                    <label class="control-label" for="titulo">Denominação:</label>
                                    <div class="controls">
                                        <div class="span12">
                                            <input type="text" name="denominacao" id="denominacao" class="input-xlarge" />
                                        </div>
                                    </div>
                                </div>


                                <div class="control-group">
                                  <label class="control-label">Arquivo</label>
                                  <div class="controls">
                                     <div class="fileupload fileupload-new" data-provides="fileupload">
                                        <div class="input-append">
                                           <div class="uneditable-input">
                                              <i class="icon-file fileupload-exists"></i> 
                                              <span class="fileupload-preview"></span>
                                           </div>
                                           <span class="btn btn-file">
                                               <span class="fileupload-new">Selecione o arquivo</span>
                                               <span class="fileupload-exists">Alterar</span>
                                               <input type="file" class="default" name="imagem1" id="imagem1"/>
                                           </span>
                                           <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                                        </div>
                                     </div>
                                  </div>
                               </div>



                                <div class="form-actions">
                                    <input type="submit" class="btn btn-primary" name="bt_save_df" value="Salvar">
                                    <a href="ger_arquivos.php"><button type="button" class="btn">Voltar</button></a>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <!-- END Main Content -->

It should be generated automatically, because I will delete the "control code" part of this option.

In case, the variable codcontrol must be supplied automatically with these rules, as I could do, completely eliminating this input?

    
asked by anonymous 23.04.2015 / 18:05

3 answers

1

A different way is to split the list of characters and use a lot of str_shuffle .

Here's an example:

$id = 'A'.substr(
    str_shuffle(
        str_shuffle('ghijkl').
        str_shuffle(6543).
        str_shuffle('ABCDEFGHI').
        str_shuffle('012').
        str_shuffle('fedcba').
        str_shuffle('ZYXWVUTSRQ').
        str_shuffle('mnopqr').
        str_shuffle(789).
        str_shuffle('JKLMNOP').
        str_shuffle('zyxwvuts')
    ),

    mt_rand(0,53),
    mt_rand(2,9)
);

The variable $id will have a random key of size random of strings arranged in random .

The strings used can be changed in the way that is preferable.

    
23.04.2015 / 21:11
2

You can do this:

<?php

function id_unico() {

    return uniqid("A"); 
}

More information about uniqid link

Example: link

    
23.04.2015 / 18:39
1

You can use a function like this:

function valorAleatorio($qtdSorteio = 5) {
    $letras = array('A','a','B','b','C','c','D','d','X'); // precisa ter 9 itens
    $resultado = '';
    while($qtdSorteio > 0) {
        $resultado .= rand(0,9); // sorteia valores entre 0-9
        $resultado .= $letras[$qtdSorteio]; // retorna do array a letra pela chave
        $qtdSorteio--;
    }

    return 'A'.$resultado;
}
    
23.04.2015 / 18:21