/ Published in: PHP
Some times you need to generate a code for a field in a db and that code can't be repeated either...
Yes, I know that if two people submit their data at almost the exact same time that it could be possible that they have the same code. OMG OMG OMG! I'll take the 0.0000000001% chance that it will happen. You could get around this by generating a separate table of pre-made codes and each time you use one you take it and mark it as used.
Yes, I know that if two people submit their data at almost the exact same time that it could be possible that they have the same code. OMG OMG OMG! I'll take the 0.0000000001% chance that it will happen. You could get around this by generating a separate table of pre-made codes and each time you use one you take it and mark it as used.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** Simple function to generate a code and check if it is already used. If so * generate a new one. * * $table the database table that you are working on * $field the field that contains the codes * $length the length of the code you want. should be less that the possibilities * $chars the string that you want to choose chars from **/ function generateCode( $table , $field , $length = 7 , $chars = "ABCDEFGHJKMNPQRTUVWXYZ2346789" ) { // generate a code // test if it has been used already $q = "SELECT ".$field." FROM ".$table." WHERE ".$field." = '".$temp."'"; $qr = dbquery( $q ); //dbquery is my function to call queries... { return $temp; // not used return it } else { return generateCode( $table , $fields, $length , $chars ); // used try again } } // the chars string above is regarded in the marketing biz as being the most easily // readable so no 5S I1 O0 stuffs
URL: http://www.itsgotto.be/cv.php