Skip to content

Instantly share code, notes, and snippets.

@avinashseth
Created July 4, 2016 09:54
Show Gist options
  • Save avinashseth/533a7a897d974b731de1284029f78568 to your computer and use it in GitHub Desktop.
Save avinashseth/533a7a897d974b731de1284029f78568 to your computer and use it in GitHub Desktop.
function generate_unique_key
(
$column_name = null,
$db = null,
$table_name = null,
$size = null
)
{
if(is_null($size) || is_null($column_name) || is_null($table_name) || is_null($db))
{
return null;
}
else
{
$unique_key = "";
$code = "";
// we are creating unique value with rand function
// rand function http://php.net/manual/en/function.rand.php
for($i=0;$i<$size;$i++) {
$code = $code . rand(0,9);
}
// here we will check if key is unique
// please refer this video, code is explained here https://www.youtube.com/watch?v=MCz-s06Pzo0
if(
is_unique
(
$column_name,
$db,
$table_name,
$code
)
)
{
// if that value is unqiue, we will return it
return $code;
}
else
{
// if that value is not unique, we will create it again
generate_unique_key
(
$column_name,
$db,
$table_name,
$size
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment