How to generate a sequential number in a PHP form, without using a database?

0

Today in my form, I have the mail.php file where I read the form data and send it by email. This form is an online order form, where registered users log in and place orders. Well, I set up everything based on research and studies on the web, I'm not a knowledgeable php language, but I understand a little. Today I got to put the code (below), to generate the random numbers with the rand () function, so as not to run the risk of repeating numbers I found this formula:

<?
$numeros = array(0,1,2,3,4,5,6,7,8,9);
$total_num = count($numeros)-1;
$numPedido = $id . $numeros[rand(0,$total_num)] . $numeros[rand(0,$total_num)] . $numeros[rand(0,$total_num)] ;
$gerador = $numPedido;


?> 

Well, solved, generate numbers without the need of Database, but would like to use sequential, how? Is there any function, in javascript, I do not know, that can save the information of the number in TXT file and then consult the file not to repeat the number?

I have already found several tutorials on using DB, but found it complicated. Since the order form is not saved anywhere, it is only sent by email, we only use the BD to load user data and of course, to have access to the order online.

This code I used put in the mail.php file. So the number appears in the body of the email and I put it to appear also in the subject.

Thanks in advance for your attention!

    
asked by anonymous 11.04.2016 / 03:44

1 answer

0

Using the database is the best alternative.

Using Javascript is impossible, this runs on the client side, not on the server, forget it.

What you can use is an alternative to the database, if you search for "PHP without DB store data" you will find similar solutions.

Idea:

Keep a .txt or .php file, for example, that simply contains some information, in this case, the information of the last value entered.

How to do it?

There are several ways.

Example:

Using include:

To save the information use:

file_put_contents('contagem.php', '<?php $contagem='.((int)$contagem+1).' ?>');

To get the information use:

include('contagem.php');

Summary:

The contagem.php will always have the variable $contagem following.

Application in your case:

Using the variable $contagem and the variable $gerador will have this:

<?

// Verifica se contagem.php existe
/* Se for a primeira vez que executa ele não irá existir. */
if( file_exists('contagem.php')){
    // Se existir inclui a variavel que armazena a informação
    include('contagem.php');
}

// Salva o proximo número no contagem.php (apagando a informação anterior e salvando o proximo número).
file_put_contents('contagem.php', '<?php $contagem='.((int)$contagem+1).' ?>');

// Exibe o $gerador que é igual a $contagem.
/* Remova o echo para ocultar obviamente */
echo $gerador = (int)$contagem;

?> 

At the end, each time you add one to the previous $ count. That way the first one will be 0 (due to (int) ) the next one will be 1, then 2,3,4,5,6 ...

Notes:

  

Note The first time you run Notice errors will be shown, because the $contagem variable does not exist, even though its value will be 0 normally. In the next few times there will be $contagem , so there will be no such error.

See more

If you want to see more about file_put_contents and include see the manual at link and link .

There are alternatives to using file_get_contents in link or using fopen , but more complex, in link .

    
11.04.2016 / 07:28