neven Posted December 20, 2011 Share Posted December 20, 2011 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/253557-random-id-for-dosie-witch-does-not-exist-in-database/ Share on other sites More sharing options...
ManiacDan Posted December 20, 2011 Share Posted December 20, 2011 Use auto-increment columns to automatically assign an ID to an item when you insert it. Also, you might be better off posting in a forum for your native language. Quote Link to comment https://forums.phpfreaks.com/topic/253557-random-id-for-dosie-witch-does-not-exist-in-database/#findComment-1299823 Share on other sites More sharing options...
Psycho Posted December 20, 2011 Share Posted December 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253557-random-id-for-dosie-witch-does-not-exist-in-database/#findComment-1299829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.