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.
No comments posted yet
Your Answer:
Login to answer
179
8
Other forums
array_combine() trouble w/csv file
I have a problem with a piece of code I wrote to import some records from a csv file into mysql. I h
Using CSS to format MySQL query
Hi,
If i want to have a news page on my site, which displays all records of a table in descen
FROM_UNIXTIME($phpdate);
Hello,
I tried to use this :
$date = FROM_UNIXTIME($phpdate);
ANd I was t
How do I get the row number from from an sql table query
Hi all
I have a table that I query and it returns a number of rows.
mysql_num_rows($query)
PHP url branch??
Functionally, using PHP, I'm doing a test of login input submitted by a user via an html form in a f
RSS feed - FeedList for WordPress
Hi,
I'm trying to alter the FeedList plugin for WordPress so that I can click on the title of
Running External Scripts
I am very new to PHP and am not even sure what I'm asking is possible. I have just installed mediaW
Setting a default timezone?
I have read about how to change the timezone in PHPMYADMIN, but it changes back, it doesn't STAY the
Parse error: syntax error, unexpected $end in
hello,
i receive the error :Parse error: syntax error, unexpected $end in C:xamppxampphtdocsh
Why is my row count 0?
Here's the MySQL query i'm running. It basically pulls data from 2 tables based on some data passed.