Jump to content

Unique account numbers


guymclaren

Recommended Posts

I have a need to create unique account numbers based on three letters and 3 numbers. I want to create a little script to check the surname. In ASP I can select the first three letters after the space eg Input "Guy McLaren" I want to select the MCL and then add 3 numbers to it. the first MCL would be 001 the next would be 002 etc.

 

Is there an easy way to do this? Or would it be simpler for me to use the unique id created in the database as a UID?

 

My initial thoughts are

1. find the space and select the letters

2. Check database for account numbers starting with MCL and check last number

3. seperate number from string

4. add one

5. join string

 

Will that be the best way?

 

 

Link to comment
https://forums.phpfreaks.com/topic/124699-unique-account-numbers/
Share on other sites

IF you do want to use a completely random number like the last poster suggested just use this function

 

function create_random_str($length){
$src = 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ@_';
$len = strlen($src) - 1;

$randstr = "";

for($i=0; $i<$length; $i++) 
{ 
	$x = rand(0,$len);
	$randstr .= $src{$x};
}	
return $randstr;
}

You don't need a loop in that function, use the shuffle() function:

<?php
function create_random_str($length){
$src = array_merge(range('a','z'),range(0,9),array('@','_'));
        shuffle($src);
return implode('',array_slice($src,$length));
}
?>

 

Ken

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.