Welcome Guest, Not a member yet? Register   Sign In
Generating Unique ID
#3

[eluser]Michael Wales[/eluser]
You'll never be able to guarantee absolute uniqueness on the insertion by generating a hash and then substringing it. It will be pretty unique - but never guaranteed.

Personally, I would just use the string helper, then loop until the query is properly executed.

Code:
function make_unique($filename = NULL) {
  if ($filename !== NULL) {
    $this->load->helper('string');
    $unique = FALSE;
    while ($unique === FALSE) {
      if ($this->db->update('files', array('id' => random_string('alnum', 5)), array('filename' => $filename))) {
        $unique = TRUE;
      }
    }
    return TRUE;
  }
  return FALSE;
}

Then just set that field as a UNIQUE key within your database engine (or even a primary, since it's the identifier). You don't have to use random_string - hashing time() and then substringing it would work, but you still need a fall-back due to the chance of nonuniqueness.


Messages In This Thread
Generating Unique ID - by El Forum - 07-06-2008, 11:14 AM
Generating Unique ID - by El Forum - 07-06-2008, 11:23 AM
Generating Unique ID - by El Forum - 07-06-2008, 12:59 PM
Generating Unique ID - by El Forum - 07-06-2008, 04:11 PM
Generating Unique ID - by El Forum - 07-06-2008, 04:19 PM
Generating Unique ID - by El Forum - 07-06-2008, 04:30 PM
Generating Unique ID - by El Forum - 07-06-2008, 06:07 PM
Generating Unique ID - by El Forum - 07-06-2008, 06:23 PM
Generating Unique ID - by El Forum - 07-06-2008, 07:32 PM
Generating Unique ID - by El Forum - 07-06-2008, 08:41 PM
Generating Unique ID - by El Forum - 07-06-2008, 10:18 PM
Generating Unique ID - by El Forum - 07-06-2008, 10:35 PM
Generating Unique ID - by El Forum - 07-06-2008, 11:48 PM
Generating Unique ID - by El Forum - 07-07-2008, 12:27 AM
Generating Unique ID - by El Forum - 07-07-2008, 12:53 AM
Generating Unique ID - by El Forum - 07-07-2008, 01:07 AM



Theme © iAndrew 2016 - Forum software by © MyBB