Welcome Guest, Not a member yet? Register   Sign In
How to generate a random serial number
#2

[eluser]jedd[/eluser]
Hi cleos and welcome to the CI forums.

You've got a pattern problem there - you need to have the function that generates the actual serial number as a separate function - perhaps a private function (start with an underscore). That way you can call it, then consult the database, ideally via a call to one of the methods in your new Model! - and repeat as necessary.

Can I ask though - what's the actual problem you're trying to solve? You've described how the solution you've come up with doesn't work .. and this is a common problem when trying to think about solutions - you start to lose sight of the original, base, fundamental (etc) problem.

For instance, I'd suggest that the table you're generating a new row for here should use an id field that's an autoincrement unsigned integer. Randomly generated anythings are expensive, and even more so if you can't guarantee uniqueness. So if you're perversely opposed to a guaranteed unique integer primary key here, and want something less integery for some other purpose - then I'd suggest either - use md5 (cheap) to generate a reasonably-likely-to-be-unique string based on the integer (and any other data you've got for that row). You may have luck with utilising some substr() of such an md5 output. It's probably cheaper to use the md5 function and repeat on duplicates than your own random number based function.

Second alternative - use UUID's - either dr uuid(?) - or one of the other php libraries for generating UUIDs - or if you're using MySQL, what I've done in the past is have a MY_Model call that asks MySQL for a UUID using its built-in. From memory, MySQL provides v5 UUIDs, so while safety can't be guaranteed, it should be reasonably unlikely you'll hit any duplicates in the next few lifetimes.


Messages In This Thread
How to generate a random serial number - by El Forum - 12-25-2010, 06:34 PM
How to generate a random serial number - by El Forum - 12-25-2010, 08:40 PM
How to generate a random serial number - by El Forum - 12-25-2010, 10:21 PM
How to generate a random serial number - by El Forum - 12-26-2010, 07:00 AM
How to generate a random serial number - by El Forum - 12-27-2010, 05:10 AM
How to generate a random serial number - by El Forum - 12-27-2010, 07:23 AM
How to generate a random serial number - by El Forum - 12-27-2010, 07:34 AM
How to generate a random serial number - by El Forum - 12-27-2010, 02:25 PM
How to generate a random serial number - by El Forum - 12-27-2010, 02:32 PM
How to generate a random serial number - by El Forum - 12-27-2010, 04:56 PM



Theme © iAndrew 2016 - Forum software by © MyBB