Last active
December 13, 2018 21:51
-
-
Save hh-com/0feefaa0f76b8d636c43ebd2992737a1 to your computer and use it in GitHub Desktop.
Contao - Create a Unique Key by a char set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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