Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random numbers vs sequential numbers

Status
Not open for further replies.

skydivelouie

Programmer
Jan 19, 2005
5
US
I need an expert who can help me out with a precise answer...I can't seem to find an answer on the net or in other forums...Please Help...

This is what I'm trying to accomplish...

$Alpha = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
$a = rand(0,25);
$b = rand(0,25);
$c = $Alpha[$a];
$d = $Alpha[$b];
$e = rand(10000,99999);
$f = rand(100,999);
$ID = $c.$e.$d.$f;

This produces a random ID alpha/numeric...

What I'm wanting to do is have it solely numeric and sequential...for example starting from 100 and leading up to 1000 in increments of 1...

I can find all kinds of information about producing random numbers...which I've done successfully by accident and on purpose...

It would seem that something like this would work...but it doesn't...

$a = 100; $a++
$ID = $a;

This only seems to return the ID 101 over and over...it doesn't not increase in increments of 1 with each form submission...I would like the number to be unique and non-repeptitive...

Does anyone know how this can be done?...I am at my wits end...

Any help, advice or a point in the right direction will be extremely helpful...

Thanks
Skydive Louie
 
You have to remember that the script runs anew every time a web browser tries to access it. The only way to generate sequential numbers is to in some way preserve the state of the values between script runs.

One way would be to write the generated sequential value just used to a text file, then have the script read that file on each run to see what the next number should be. Problems with this is that your script could be run multiple times simultaneously, so you are going to have to code in file locking to insure that only one instantiation of the script is changing the state file at any give time. See flock() for more information.

If you're going to store this unique value in a database, you may be able to have the database server generate the sequential parts of the string. MySQL, for example, provides the auto_increment attribute for integer column types. You could create a new record in the table, fetch the sequential number generated, then used that number in your string, storing it back into the table with other information.

If the sequences don't have to be necessarily sequential, you might look at PHP's uniquid() function.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for responding.

I was advised that possibly a flat file would suffice and be easier to implement.

I am not very experienced in this and am still learning, therefore I'm looking for the easiest possible solution.

Would these infact be easier and just as effective?
 
Insufficient data for a meaningful answer.

What are you going to be doing with these IDs once you've generated them? If you're going to be inserting them into a table on a database server, then having the database server is probably easier. After all, you're having to communicate with it any way.

If you're not going to be doing database manipulations, then a flat file might work for you. However, keep in mind my warning about problems with concurrent instantiations of the script and how to work around them using file locking.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Here's a copy of the flat file funtion of the scripts:

if ($Action == "F" or $Action == "EF" or $Action == "DF"){

while (list ($key, $val) = each ($_FILE))
{
if ($key != "Settings" and $key != "Refresh")
{
$val = stripslashes($val);
$val = str_replace('"','"',$val);
$val = strip_tags($val);
$key = ucfirst($key);
$key = str_replace("_"," ",$key);
$FileData .= "$key: $val\r\n";
}
}

$FileData .= "Received: " . $Time . "\r\nIP: " . $IPAddress . "\r\n" . $FileData . "\r\n========================================\r\n\r\n";

$handle = @fopen($FlatFile,'a');
@flock($handle,LOCK_EX);
@fwrite($handle,$FileData);
@flock($handle,LOCK_UN);
@fclose($handle);

} // END OF ACTION

There is also a second settings.php file for easy manipulation, although not so easy for me. This is what I've done with that portion.

# FLAT-FILE SETTING
$FlatFile = "/mmex/ID.txt"

Also, I CHMOD to 666 for this file.

I am using this script as a form processor. I am attempting to generate a unique Order ID for each form submission. The form is sent in HTML to a specified email address with the Subject line being: Web Order - 1000. A second form submission would be sent to the same email address, although this time the subject line would be: Web Order - 1001. And so on.

I don't need to do anything with the number sequence, other than be certain that the numbers do not duplicate.

These will be in my inbox and I would access these form submissions from there.

I apologize for my ignorance and appreciate your help.
 
it's much easier using a mysql database.
1: show input form
2: validate input data
3: insert in db and send to administrator
4: show confirmation screen to user and mail to user

then, the admin also has a database containing input.
this could also be a two-way thing.. not very complex.
eg. the admin could answer the visitor, and they both got reciepts.

then the admin could also look at older enqueries from the visitor, if there are some.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top