• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

How to create a random number ?

javier94

Member
hello,

i have a registration form and i would like add an a hide element, where when the user's makes the registration a random number for this user would be generated automatically..

the point is ... use this random number in some forms if is needed, instead of name or surname..

Which element would have to use to do that??? and how i have to do it.?? maybe i have to use javascript??

i would need a random number of 9 numbers and letters as for example "X678VBju8"

thanks in advance!!!
 
Easiest way is probably to use a field element, set to hidden, with an eval'ed default of something like ...

Code:
return substr(md5(uniqid()), 0, 9);

... which will produce a reasonably random string, although it'll be lower case letters only. Here's a couple of examples, running it in my debugger:

Code:
??> substr(md5(uniqid()), 0, 9)
?< a340f01ed
>?? substr(md5(uniqid()), 0, 9)
?< 8c35ae4c8

If you want lower and upper case ... try ...

Code:
return substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);

... which looks like ...

Code:
??> substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
?< LeK4z1muw
??> substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
<? Jr7pB7XKU

Of course, with either of these methods (or any "random" string generator) there is a chance, albeit vanishingly small, that you may wind up with a duplicate. Statistically it's in the billions (or trillions, or billions of trillions) to one against a chance of happening ... but could happen.

If that's a worry, you'd have to add some code to this to check to see if your string has been used before ...

Code:
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$oops = 0;

do {
   // these 3 lines are just here to prevent locking up your web server in an infinite loop doing database lookups
   // if I made a mistake in the code, you can remove them once it's working
   if ($oops++ > 10) {
      return "Something went wrong!";
   }
   $myString = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
   // replace 'yourfield' with this element's name, and 'yourtable' with this form's table name
   $myQuery->select('COUNT(*) AS foo')->from('yourtable')->where->('yourfield = ' . $myDb->quote($myString));
   $myDb->setQuery($myQuery);
   $myResult = $myDb->loadResult();
} while (!empty($myResult));

return $myString;

-- hugh
 
IGNORE this - I missed the mt_rand() hugh included. That's what you get for reading forums on a small phone screen. Shudda known Hugh would have considered that.

>>You might want to use one of the rand() functions instead of str_shuffle. I've seen comments that str shuffle is not recommended for cryptography. Course even rand is not really random. If you want that you can go to services like random.org.>>

All depends on how particular you are .
 
Last edited:
Yup, I made sure I added some randomness. :)

And no, even mt_rand() isn't "cryptographically" random, for that you'd need something like openssl_random_pseudo_bytes(), although even that isn't REALLY random (as far as crypto purists are concerned), but I figured the entropy in the suggestions I gave him were enough for a signup form. :)

-- hugh
 
with the two last post.. I'm lost.. sorry but i didn?t understand clearly..

i try with that... but maybe something is wrong.. the element dont makes nothing

$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$oops = 0;

do {
// these 3 lines are just here to prevent locking up your web server in an infinite loop doing database lookups
// if I made a mistake in the code, you can remove them once it's working
if ($oops++ > 10) {
return "Something went wrong!";
}
$myString = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
// replace 'yourfield' with this element's name, and 'yourtable' with this form's table name
$myQuery->select('COUNT(*) AS foo')->from('yourtable')->where->('yourfield = ' . $myDb->quote($myString));
$myDb->setQuery($myQuery);
$myResult = $myDb->loadResult();
} while (!empty($myResult));

return $myString;

maybe i have to add or delete something ???
 
i'm trying this but i'm not sure what happend.. nothing appears in the element.. you can see here.. last element

https://www.selectiumlab.com/index.php/ca/candidats

i have created a Field element inside the form.. named .. randon number

for the moment is "not" hide till see the number ... in predeterminated... the code.. and "Eval " button --> YES

form data base is: users
random element is: users___random_number

the code:
----------------------------------------------------------------------------
$myDb = JFactory::getDbo();
$myQuery = $myDb->getQuery(true);
$oops = 0;

do {
// these 3 lines are just here to prevent locking up your web server in an infinite loop doing database lookups
// if I made a mistake in the code, you can remove them once it's working
if ($oops++ > 10) {
return "Something went wrong!";
}
$myString = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
// replace 'yourfield' with this element's name, and 'yourtable' with this form's table name
$myQuery->select('COUNT(*) AS foo')->from('users')->where->('users___random_number = ' . $myDb->quote($myString));
$myDb->setQuery($myQuery);
$myResult = $myDb->loadResult();
} while (!empty($myResult));

return $myString;
---------------------------------------------

Any mistake here? i understand be hide or not.. should not affect..
 
A number of problems:

There was a typo in my example code. I had ->where->() instead of ->where().
You had the wrong field name - you used the "full" Fabrik element name (with the table name prepended), users___random_number, instead of just the field name, random_number.
Your list is not on the default site connection, so need to use FabrikWorker::getDbo(false, 2), to get that specific database connection, not the default J! one with JFactory::getDbo(). This will apply to any custom code you write for this app that needs the database.

Working code is:

Code:
$myDb = FabrikWorker::getDbo(false, 2);
$myQuery = $myDb->getQuery(true);
$oops = 0;

do {
   // these 3 lines are just here to prevent locking up your web server in an infinite loop doing database lookups
   // if I made a mistake in the code, you can remove them once it's working
   if ($oops++ > 10) {
      return "Something went wrong!";
   }
   $myString = substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', mt_rand(1,10))),1,9);
   // replace 'yourfield' with this element's name, and 'yourtable' with this form's table name
   $myQuery->select('COUNT(*) AS foo')
    ->from('users')
    ->where('random_number = ' . $myDb->quote($myString));
   $myDb->setQuery($myQuery);
   try {
    $myResult = $myDb->loadResult();
   }
   catch (Exception $e)
   {
    //$this->app->enqueueMessage($e->getMessage());
    return 'Something went wrong!';
   }
} while (!empty($myResult));

return $myString;

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top