Unique ID [GH] action for CF5

mfeindt 15 May, 2018
Hi,

I'm looking for the "Unique ID [GH] action" for CF5. I have already sent a PM to Bob (GreyHead) but have not received a reply yet. Is there another way to get the action?

Kind Regards
Matthias
mfeindt 15 May, 2018
Answer
Thanks GreyHead. Nice work!


If someone else need the uniq_id to be accessible in $form->data[], try the action from GreyHead.


Or try the following, which was my attempt:

Insert custom code section before your db save action.
<?php
/* ********** $form->data['uniq_id'] ********** */
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('a.uniq_id');
$query->from('#__tablename AS a');
$db->setQuery($query);
$uniq_ids = $db->loadColumn();

function gkCreateUniqId(){
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$count = mb_strlen($chars);
$length = 40;
for ($i = 0, $result = ''; $i < $length; $i++) {
$index = rand(0, $count - 1);
$result .= mb_substr($chars, $index, 1);
}
return $result;
}

do{
$form->data['uniq_id'] = gkCreateUniqId();
} while(in_array($form->data['uniq_id'], $uniq_ids));
?>

adjust tablename and variable $length (and $chars) to your needs.

Kind Regards
Matthias
This topic is locked and no more replies can be posted.