How to generate random number for server variable

How can i generate random number to server variable using the formater if possible?

depends, what you want, how long you want the random number or if it really does not matter, I often use now.toTimestamp() because as far as I am aware the chances of having a duplicate of a timestamp ever is pretty low. I think, looking forward to seeing other peoples solutions though.

1 Like

This is the code I use to generate a random 10 digit ID number for our clients.

<!-- Generate Random ID Starts --->
<?php
$number_of_digits = 10;
$newDID2 = substr(number_format(time() * mt_rand(),0,'',''),0,$number_of_digits);
?>
<!-- Generate Random ID Ends --->

I then bind newDID2 value to a hidden form field. In my form action I also check to be sure that value doesn’t already exsist before submitting.

Hope that gives you a bit of direction.

2 Likes

i was hoping for wappler formater can do it before doing manual solution :slightly_smiling_face:

But great way thanks!

The way i showed can be done with only the Wappler formatters, no manual additions at all, i used to have a php script to do this once upon a time, but found this simpler, because almost all the Wappler and DMX Extensions had NOW() under their globals, and within the Query Builder, the database insert step etc. so I was not trying to bind it anywhere.
I hope there is no downside to me doing it this way even if not the ultimate way to achieve it, @Brad who has been doing this for years and years, can you confirm, I do not want to continue doing it this way if there might be an issue down the line.

if 2 add insert a record on the same second

I wish, any of my websites would ever have that kind of traffic, in my wildest dreams. Lol, but yes you are correct that is a possible side effect.

but your solution is 100% using the formater and that’s what i asked for
i will add count to the number of records on the database and +1
this will make it even better

hmm i like that, i might steal that idea from you, thanks @mrbdrm

1 Like

I actually kind of like that timestamp method. I may use that in the future. Curious as to how you strip out the spaces and special characters out of the timestamp though to create a number? Also in my use case I had to specifically have a 10 digit ID to correspond with government guidelines.

You have peaked my interest. It would indeed generate a random id every time.

Well well, thanks, that made me happy, so now you owe me one @brad, can i ask for payment in the form of you helping me test what i think is a bug in the query builder, the Wappler guys have just kind of not said if it is a bug or not a bug, or if it is happening to anybody else.

I have never seen any spaces or special characters when using this I have to admit, it outputs something like this 1393628400000, i think if i recall correctly i had one time when i had to add NOW().toTimestamp.toNumber but i cant recall off hand.
Maybe to make it 10 characters you could divide it by a set number because it should always be different to start with.

Timestamp doesn’t have spaces
You could use word count or use regex or pattern if possible

I was worried about stripping with things like substr or truc etc. because i did not want to cause duplication, and because i can not physically read the timestamp, i am not sure that if i had stripped lets say the last 3 characters, if it would have output as 18/03/2018 10:00:00 multiple times, so for an entire hour all timestamps would be duplicate of each other. Hope I am making sense, that is why i did the divide thing rather.

You would have to strip characters from the front of the timestamp. Now I gotta experiment with this today. So much for getting work done, :wink:

haha, lol, please keep us informed on your results

You know there is timestamp available without and conversation needed.

Can you give a hint on this? It seems to always insert into my database with date and time formatting?

{{TIMESTAMP}}
Its under the variables with the formater

Lol, nope, just seen it right this second, never noticed it there before.
I have a varchar(15) in my database then i just insert NOW().toTimestamp and it outputs 13 characters without date formatting.
The only time i have ever run into trouble was when I wanted a single order number for about 15 row inserts, I wanted all the rows from the same order to all have a duplicate order number, well as a timestamp. 9 out of 10 times it worked as expected, however every now and again the insert would take too long and 14 rows would have one number and the 15th would have another.
In this case I had to bind NOW().toTimestamp into a hidden input on the form and send the $_POST input to the insert.