randomize an id row name

tutux

Member
Hi

I would like to get the id of a new row throught form process, and save it in a new table's element with a random name containing the id record.

I explain : this php code takes the id number (id=1), rename it in "AAAA-1", and save it in reference_articles table :

PHP:
$rowid = $formModel->getRowId();
if (!empty($rowid)) {
   $db = JFactory::getDBO();
   $newNumber = "AAAA-" . $rowid;
   $query = $db->getQuery(true);
   $query->update('core_articles')->set("reference_articles = '$newNumber'")->where("id = $rowid");
   $db->setQuery($query);
   $db->query();
}

But i would like to have a random rename like BTFR-1 or ZGVX-1 instead alway AAAA..

any ideas?
 
If you want something that would pretty much assure a unique random rename you might try using the php microseconds function
e.g.
$newNumber = 'R'.round(microtime(true)*1000).'_'.$rowid;
For row id 23, this would return something like
R1132470321083_23
 
Thank you Bauer !
yes your solution works fine! but the result is a little bit too long :)
, is there a way to do that with caracters? i saw on web many codes for that, but i really don't know how to insert it in my fabrik plugin :)

for exemple :

PHP:
<?php
    $characts    = 'abcdefghijklmnopqrstuvwxyz';
        $characts   .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';   
    $characts   .= '1234567890';
    $code_aleatoire      = '';

    for($i=0;$i < 10;$i++)    //10 est le nombre de caract?res
    {
        $code_aleatoire .= substr($characts,rand()%(strlen($characts)),1);
    }
echo $code_aleatoire;
?>
 
That code (less the php tags and the last 'echo...' line) could just go as the first lines in your if condition in your initial example.

Then change the line
$newNumber="AAAA-".$rowid;
to
$newNumber=$code_aleatoire."-".$rowid;
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top