Pardon the framework specific code. Please just take my word that the objects do what I say they do.
My Code
//Generates a random five digit alphanumerical id
$aliasId = $model->random_id_gen('5');
//calls the active record class for table Person
$person = new Person();
//searches the person table to see if this alias is being used
$search = $person->find('alias=:alias', array(':alias'=>$aliasId));
//if null then it sets an attribute for a another active record class
if ($search==NULL)
{
$model->setAttribute('alias', $aliasId);
$model->setIsNewRecord(TRUE);
}
else
{
//I need to loop through the above code until I find an alias that isn't being used
}
My Question
What do I write in the else statement to run through the code above until I find an alias that isn’t being used in the Person table. My guess is some kind of loop, but I’m just not really sure how to do it. Feel free to re-work this how you like. Put it as its own function/tell me I’m doing it wrong, I won’t be offended. Thank you SO!
However, it may be a better idea to use an auto-incremented value for the alias-id if it does not have to be randomly generated.
To convert the number to and from base36 you can use PHP’s base_convert function:
output:
If you want to make sure that your number is at least 5 digits start your increment value at:
base_convert(10000,36,10)= 1679616