How to generate random number for server variable

And why dont you generate it ones and just insert the value to the rest?

Because I was using it as a value in the insert step which was just easier, so my table was chosen in the insert step and brought in 10 fields, so i linked them with
fieldA > value {{$_POST.fieldA}}
fieldB > value {{$_POST.fieldB}}
fieldC > value {{$_POST.fieldC}}
fieldD > value {{NOW().toTimestamp()}}

so fieldD was my order number

then when i saw the problem, I did what you are suggesting, a hidden field in the form pulling in the timestamp and then inserted that once to my database.

I am having serious ‘old person brain’ today. Can’t find anyway to bind a timestamp to a form field? It should be easy…

Hi Brad,
No need to bind it to a form field. It is available directly in server action, under globals so when you pick a value to be inserted/a value to work with - just select it in the dynamic data picker.

No need to use hidden fields anymore
Do your server actions on the server :sweat_smile:

Although your suggestions cover the answer, just posting what I do to create a random string just in case it brings up more ideas. After creating a random string I add the current time. This makes it virtually impossible to have the same string generated twice in the same second.

  <?php function generateRandomString($length = 40) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
    $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
    }
    echo $cook= generateRandomString().((isset($_SERVER["REQUEST_TIME"]))?$_SERVER["REQUEST_TIME"]:"");
    ?>

You already can generate a unique long number with one line:

{{TIMESTAMP+yourQuery.count()+1}}

No need for lengthy PHP code my friend.

3 Likes

For a really unique value you could also use the query.count()+1, hash it using md5 method and use timestamp value as a salt :slight_smile:

2 Likes

And this is why we need the community forum :slight_smile:

1 Like

Won’t this eventually result in very long results? The timestamp is ten digits plus adding the count each time if you have 4000 entries you now have a 14 digit value? Or am I misunderstanding it?

Your adding as number not string

1 Like