Jump to content

Random id for dosie witch does not exist in database


Recommended Posts

I'm trying to generate a unique "dosieid" number for my web site.My web site is a human resources program solution, in that program users create dosie of their workers in their firm ...random dosieid needs me so when user createing dosie in field dosieid automaticly show the dosieid-s that are not used before...the dosieid that don't exist in database. In other case I would use auto increment but in this case dosie is not created yet. And in form dosieid must be option to change the number if random is not fine with a user. One more hint the numbers must bee from 1 to 9999. Can someone help mee? I have try many codes but i have not find something like one with this spec.

 

This is what I have do so far. It gets the random number but I don't know how to compare that random number with database row "dosieid" ?

 

 

<?
                                                                    $id_num = mt_rand(1,9999);

							    $query = "SELECT dosjeid FROM albums";

							    $result = mysql_query($query) or die(mysql_error());

							    while($account = mysql_fetch_array($result)){

							        if ($id_num == $account['id']){

							    $id_num = mt_rand(1,9999);
							    }							    
							}	
							echo"$id_num<br>";
?>

Although I agree with ManiacDan that you should be using an auto-increment field for the internal, primary ID. I've worked with applications where there is a user-facing ID. This is typically used for quick selection of a user when assigning other objects to. Although I have seen where the internal, primary ID and the user-facing ID are the same I would highly advise against this.

 

If your requirements are that the user-facing IDs can ONLY be from 1 to 9999, then I don't think it would be too much of an issue to manage those IDs separately from the primary ID. Here is a quick and dirty method. It isn't the fastest method, but the process of assigning/changing these IDs should not be an everyday occurrence.

 

I would create a table of "available_ids" and populate it with values from 1 to 9999. Then whenever a new user is created you could pick a random value from the "available_ids" and assign it to the user. Note: You don't need to delete IDs that are used since you can simply JOIN the "available_ids" table to the users table to dynamically find the ones that are not in use.

 

Sample query for getting unused available IDs:

SELECT id
FROM available_ids
LEFT JOIN users ON users.user.id = available_ids.id
WHERE users.user.id IS NULL

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.