Created
July 4, 2016 09:54
-
-
Save avinashseth/533a7a897d974b731de1284029f78568 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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