talkinggoat
Member
I am trying to make a field that will use php to generate a unique URL... but I have to search the list/table it's stored in, to make sure the URL does not exist, before the data is generated. I'd rather do it before, than use validation and tell the person that something went wrong. To accomplish this, I believe I'll need to use a mysql query to run through all the data, using a while loop. If the data exists, it simply repeats the unique URL generation script, below.
I'm not sure if a raw php query of the table is the correct way to do this or if Fabrik has a better way.
PHP:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
return 'http://mywebsite.org/'.generateRandomString();
I'm not sure if a raw php query of the table is the correct way to do this or if Fabrik has a better way.