Revision: 43971
                            
                                                            
                                    
                                        
Updated Code
                                    
                                    
                                                    
                        at April 8, 2011 00:37 by brownrl
                            
                            Updated Code
/** 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
	$temp = substr( str_shuffle( $chars ) , 0 , $length );
        // 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...
	if( mysql_num_rows( $qr ) == 0 )
	{
		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
                                
                            Revision: 43970
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at April 4, 2011 18:35 by brownrl
                            
                            Initial Code
function generateCode( $table , $field , $length = 7 , $chars = "ABCDEFGHJKMNPQRTUVWXYZ2346789"  )
{
	$temp = substr( str_shuffle( $chars ) , 0 , $length );
	
	$q = "SELECT ".$field." FROM ".$table." WHERE ".$field." = '".$temp."'";
	$qr = dbquery( $q );
	if( mysql_num_rows( $qr ) == 0 )
	{
		return $temp;
	}
	else
	{
		return generateCode( $table , $fields, $length , $chars );
	}
	
}
                                Initial URL
http://www.itsgotto.be/cv.php
Initial Description
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.
Initial Title
PHP generateCode
Initial Tags
php
Initial Language
PHP