Skip to content

Instantly share code, notes, and snippets.

@hh-com
Last active December 13, 2018 21:51
Show Gist options
  • Save hh-com/0feefaa0f76b8d636c43ebd2992737a1 to your computer and use it in GitHub Desktop.
Save hh-com/0feefaa0f76b8d636c43ebd2992737a1 to your computer and use it in GitHub Desktop.
Contao - Create a Unique Key by a char set
<?php
public static function generateUniqueId()
{
$charSet = "ABCDEFGHJKLMNPQRSTUVWXYZ123456789";
$position = 0;
$uniqueId = '';
for ($i = 0; $i < 6; $i++)
{
$position = ($position + rand(1000, 9999)) % strlen($charSet);
$uniqueId .= $charSet[$position];
}
# UNIQUE ID muss einzigartig sein und darf nicht bereits in der Datenbank vorhanden sein
$uniquIdResult = \Database::getInstance()
->prepare("SELECT * FROM tl_member WHERE uniqueid like ? ")
->execute( $uniqueId );
if ($uniquIdResult->numRows > 0 )
{
$uniqueId = self::generateUniqueId();
}
return $uniqueId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment