Alphanumeric generator in form

Asked

Viewed 57 times

1

I have developed an alphanumeric generator routine to be placed in a form field. However, it does not generate the numbering. Someone can help?

That field below:

<div class="guiaprest">
            <label name="tPrest"> 2- Nº Guia no Prestador</label>
        </div>

So I did some research on the Internet, and I found a model and I did this:

<?php
function rand_sem_num_repetido($qtd_numeros,$limite_min,$limite_max){
    for($i=0;$i<=$qtd_numeros;$i++){
        $aux=rand($limite_min,$limite_max);

        if($i>=1){
            while(in_array($aux,$index)){
                $aux=rand($limite_min,$limite_max);
            }
        }

        $index[$i]=$aux;
    }

    return $index;
}
?>
  • Numeric alpha is letters+nums right? There you are just generating numbers

  • Yes. True. I hadn’t noticed it. But even though it’s just numbers in the code above, it doesn’t generate anything.

  • You have to call the function. I ran well the code you put, maybe it just lacks the implode(), I will answer below

  • Okay Miguel, I’ll be waiting.

  • A question: rand_sem_num_repetido This is because there’s supposed to be the same number repeated?

  • Yes. When generating a new sequence, it cannot be the same as the previous one.

Show 1 more comment

1 answer

0


function rand_sem_num_repetido($qtd_numeros,$limite_min,$limite_max){
    $index = array();
    for($i=0;$i<$qtd_numeros;$i++){
       $aux=rand($limite_min,$limite_max);

       if($i>=1){
           while(in_array($aux,$index)){
               $aux=rand($limite_min,$limite_max);
           }
       }

       $index[$i]=$aux;
    }
    return implode($index);
}

Try it this way. But this function does not generate an alphanumeric, and this parameter $qtd_numeros will not limit the number of characters can come 2 characters but 1 number. (Ex.: 10 (Ten)). To limit the number of characters you can change the return to return substr(implode($index), 0, $qtd_numeros); or pass $limite_min = 0 and $limite_max = 9.

  • A question, I should create a file with this code and make the call in html or include direct in html?

  • You can create a file, 'functions.php' to be more organized. But if you prefer you can put it in the . php file directly. It works both ways. But in a file . html does not work.

  • Well,. I made his call inside my file and it still didn’t work.

Browser other questions tagged

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