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

Asked

Viewed 2,935 times

0

Today in my form, I have the mail.php file where to read the form data and send by email. This form is an online order form, where users already registered Ogan and place orders. Well, I’ve assembled everything based on research and studies on the web, I’m not a connoisseur of the language in php, but I understand very little. Today I came to put the code (below), to generate the random numbers with the function Rand(), 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, you did, generating numbers without the need for a database, but you’d like to use them sequentially, is there a way? It has as some function, in javascript, I don’t know, that can save the number information in TXT file and then consult the file not to repeat the number?

I’ve found several tutorials on the use of comics, but I found it complicated. Since the order form is not saved anywhere, it is only sent by email, we only use the BD to upload the user’s data and of course, to have access to the online order.

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

Thank you for your attention!!

  • Where this number will be used?

1 answer

0


Using the database is the best alternative.

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

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 file . txt or . php, for example, that simply contains some information, in this case the information of the last inserted value.

How to do?

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 next.

Application in your case:

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

<?

// 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 will be added one to $previous count. That way the first will be 0 (due to the (int)) the next will be 1, then 2,3,4,5,6...

Notes:

Note the first time you run will be shown Notice errors, because the variable $contagem does not exist, even then its value will normally be 0. Next times there will be the $contagem, so there will be no such mistake.

See more

If you want to see more about the file_put_contents and the include see in the manual on http://php.net/manual/en/function.file-put-contents.php and http://php.net/manual/en/function.include.php.

There are alternatives to using the file_get_contents in http://php.net/manual/en/function.file-get-contents.php or using the fopen, but more complex, in http://php.net/manual/en/function.fopen.php.

  • My friend, helped and very much! Thank you very much for your attention, but just one thing, this.php count file can be reset per day?

  • You can use a new variable with date() inside it, and compare it to an external one (in the same file that has the $generator).

  • Thanks, I’ll try here! I’m very raw in PHP, I have a lot to study yet...rs. But thanks really my friend!

Browser other questions tagged

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