Count the number of sended forms - how to?

Hello,

I would like to know your options on this subject please.

I have a request in hands that states:

Each time we receive an online form data on our e-mail, we would like to “attach” to that mail message, a number.

On the first form filled and sended we would like to have 1000, on the second form sended we would like to have 1001, on the third 1003.

This form as no database connections at all, and I assume that having a database just for this is not the best solution.

What would you suggest on this case, in order to attach to every sended form a number, and increment that number each time a new form is sended?

Please advice,

Thanks in advance,
Márcio

If you absolutely cant have a database, and you’re only going to be storing the single value, you could simply read/write a file that contains the number (and then increments it before writing back, obviously) to determine the number to attach.

Thanks. :slight_smile:

I will search more info about the procedure and will post back if any doubts.

Best regards,
Márcio

Hello again,

I have this at this moment:

function leSomaTxt($ficheiroTxt)
{
    
    //abre o ficheiro passado como argumento na funcao.  - r+ abre para leitura e escrita.
    $fh = fopen($ficheiroTxt, 'r+');

    //lê X bits do ficheiro dado. Como não sabemos quantos bites vai ter o ficheiro, na altura em que a função será chamada, dizemos
    //para ler X bits, onde X é o valor dado pela função filesize.
    $dados = fread($fh, filesize($ficheiroTxt));

    //adicionamos um valor
    $dados = $dados+1;

    //escrevemos a nova informação
    fwrite($fh, $dados);

    //fechamos o ficheiro
    fclose($fh);

    //devolvemos o valor de dados para posterior utilização.
    return $dados;

}

Issue: the php compiler (do we called like this?) is not taken the value of the given txt file as a number. Instead is getting is value as a string.
Is there a way so that we can force him to understand that the 1 on our txt file is a number and not a string.

thanks.

    $dados = fread($fh, filesize($ficheiroTxt));

=>

    $dados = intval(fread($fh, filesize($ficheiroTxt)));

Any other comment like, your function is dummy or something is more then welcome.

I will give it a try. And a look to intval :wink:

Thanks a lot,
Marcio

No luck.

Let’s say i’m starting at 1000.

I’m getting for the first time:
1001

But, for the second time:
10011002

is the issue here?

$dados = $dados+1;

:s

thanks in advance,
m

Your problem is your method.

Here’s what your program is doing.
Say you start with a file, that has 1001 in it.
Your code says to do this:
Open File for Reading and Writing.
Read File (Pointer for file is now at the end)
Write file (Writing 1002 into the end of your file)
Close file.

Try adding this line between the reading and writing:


rewind($fh); #I dont know if this is actually necessary, given the following line, but it cant hurt
ftruncate($fh,0);

Thanks a lot, I believe I just found one solution as well:

Close the file, increment the value, and open the file again.
Because, when we open the file at a given lenght the cursor is placed at the end of the line, and probably that is the reason, right?

This works:

function leSomaTxt($ficheiroTxt)
{
    
    //abre o ficheiro passado como argumento na funcao.  - r+ abre para leitura e escrita.
    $fh = fopen($ficheiroTxt, 'r+');

    //lê X bits do ficheiro dado. Como não sabemos quantos bits vai ter o ficheiro, na altura em que a função será chamada, dizemos
    //para ler X bits, onde X é o valor dado pela função filesize.
    //para além disto, converte o valor retornado em inteiro.
    $dados = fread($fh, filesize($ficheiroTxt));

    //fecha a primeira vez.
    fclose($fh);

    //adicionamos um valor
    $dados += 1; //++$dados //ou $dados=dados+1;
    
    //abre novamente o ficheiro
    $fh = fopen($ficheiroTxt, 'w+');

    //escrevemos a nova informação
    fwrite($fh, $dados);

    //fechamos o ficheiro
    fclose($fh);

    //devolvemos o valor de dados para posterior utilização.
    return $dados;

}

If you think that it could be better on something, please let me know, I will investigate.

Thanks a lot for your time it as give me a nice push! :slight_smile:
Márcio

Closing and opening the file again is just as valid a solution. (Possibly a microsecond slower, but who’s really counting when you’re not sending out millions of emails a day)