generating random codes
Posted on
16th Feb 2014 07:03 pm by
admin
Hi guys n gals,
I have a requirement to generate 250,000 unique codes...
These codes are for a tracking service and will be stored in the database (table field has a unique index).
Now I am working on the assumption this must work on the command line as the script will take a long time to complete... here is the salient pat of my code...
Code: <?php
...
function appendToCode( &$val , $key , $append )
{
$val = $val . $append;
}
$chars = "ABCDEFGHJKLMNPQRSTUVWXYZ";
$charend = strlen ( $chars );
$date = date ( 'md' );
$month = $date[0].$date[1];
$year = $date[2].$date[3];
$datecode = $chars[$month] . $chars[$year[0]] . $chars[$year[1]];
$codes = array();
$no_codes = isset( $_GET['no_codes'] ) ? $_GET['no_codes'] : 250000;
$codecount = 0;
do
{
for ( $j = $codecount ; $j < $no_codes ; $j++ )
{
$trackcode = NULL;
for ( $i = 0 ; $i < 7 ; $i++ )
{
$trackcode .= $chars[rand( 0 , $charend - 1 )];
echo ( time() - $start ) . PHP_EOL;
}
$codes[] = $trackcode . $date;
}
//$t = count($codes);
//echo 'Codes Generated: ' . $t . PHP_EOL;
//echo 'Last Code: ' . $codes[--$t] . PHP_EOL;
$codes = array_unique( $codes );
$codecount = count ( $codes );
//echo 'Unique Codes: ' . $codecount . PHP_EOL;
// the following statement prevents infinite loop in dev mode...
if (
(time() - $start) > 150
)
{
break;
}
} while ( $codecount < ($no_codes) - 1 );
...
The last characters of the code relate to the date - as these codes will only be developed every now and then - not a frequent occurrence and will allow a check on the database to see if codes ending in that 3 character string exist...
What I am asking is, is there a more efficient method of performing this operation.
Any tips would be most welcome.
179
8
Other php-forum
How do I give a developer access to a specific directory and nothing else?
I want to give a develop access to a specific directory and nothing else
I can give them an F
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_
Can anyone view my code and tell me why im getting the error:
Parse error: syntax error, unexpect
Snapshot from video
anyone knw how to make a snapshot maker from avi n mkv format.
for example if i give a direct lin
Edit MySQL Row Using PHP and HTML Form
Hello,
Here's what I'm trying to do. Build a page where a user enters a MySQL row number in.
Time script, set to my time zone?
I am using this line to get the date and time
Code: $time = date(F." ".d.", &q
PayPal Redirect Issue
When I have my paypal button set so that is redirects to http://empiremarketing.ca/order.php,it work
Echoing a Variable from a Object
How do I get a variable from the new User Class to echo out in this clasS?
class MyApp
{
upload only text files
hi guys i want to upload only text like like (pdf,note pad and ofiice files) so can any one please t
WELCOME SCREEN
first of all let me tell you what does my script do,
it´s a very simple query to show a e
Rand() help needed
Hi all,
Can someone explain and give me a quick example of how I would go about this?