Brooke.
Brooke.

Reputation: 3731

generate get next unique number from mysql ID

I need to generate a unique extension for each user (this is separate from the ID but works much the same) the number should be 100 or greater and can be overwritten/custom set. Right now I am taking the next id and if it's less than 100 add 100.

So if the next id is 5 the number would be 105 but it the next id is 105 the number would just be 105. The problem is that because i'm letting the user pick their own extension if a user choose 105 before I want to make it automatically jump to the next number in this case 106. Now if there is 105, 106, and 108 I would want to jump to 107 then jump to 109. Here is the code I'm using to generate the numbers. I think the problem is with my while loop. I'm not sure how to make it keep on checking for unique numbers.

Here is the code, I'm sure I'm overcomplicated things A LOT.

$result = mysql_query("SELECT MAX(id) 
                         FROM USERS");
$row = mysql_fetch_row($result);
$sql_extention = intval($row[0]);

//make sure it's at least 100
$extension = ($sql_extension < 100) ? $sql_extension+100 :  $sql_extension;

//check to see if the extention is in use
$qry = "SELECT `extention` 
          FROM users 
         WHERE extention = '$extention'";
$result2 = mysql_query($qry);

//if the extention is in use then find the next available one (this isn't currently working)
if($result2) {
  //get all results greater or equal to our extention
  $qry3 = "SELECT `id`,`extention` 
             FROM admin_users 
            WHERE extention >= '$extention'";

  $result3 = mysql_query($qry3);
  //this loop needs to be rewritten somehow to get the next number by checking if the next number exist if not return that as the extention
  $new_extention = $extention+1;

  while($extention_data = mysql_fetch_array($result3)) {
    if($new_extention != $extention_data['extention']+1) {
      $extention = $new_extention;
    }

  $new_extention++;
}

Upvotes: 2

Views: 1787

Answers (3)

Sabeen Malik
Sabeen Malik

Reputation: 10880

I came up with this, haven't tested it thoroughly but i think it should return the next available value correctly

SELECT (a.extention + 1) as avail 
FROM admin_users a LEFT JOIN admin_users b on ( (a.extention + 1) = b.extention )
WHERE a.extention >= 105 and b.extention is null
ORDER BY avail ASC 
LIMIT 1

So if this works as expected you wont need the last few lines of code at all.

Edit: Revised the query because i realized i was approaching it from the wrong side.

Upvotes: 2

Mel
Mel

Reputation: 6157

Ok, so you need the next available extention higher then a given number, that is not already in the database. So, you ideally want an array from the database that has all available extentions higher then a given key sorted ascending. Then you loop from the given number increasing by one, until it does not match. You don't mention a max number of extention. I would do it like this:

<?php
$rs = mysql_unbuffered_query("SELECT extention, MAX(extention) FROM admin_users WHERE extention > '$extention' ORDER BY extention ASC");
$unavailable = array();
$max = 0;
while( ($row = mysql_fetch_row($rs)) )
{
    $unavailable[] = $row[0];
    if( !$max ) $max = $row[1];
}
mysql_free_result($rs);
// Optimization for the quite common case where no more extentions are available
if( count($unavailable) > 0 )
{
    while($extention <= $max+1)
    {
        if( !in_array($extention, $unavailable) )
            break;
        $extention++;
    }
}
else
    $extention = $max+1;
// Worst case: $extention now is max + 1 and we looped through almost everything.
?>

Upvotes: 1

Dan
Dan

Reputation: 592

Shoddy attempt at PHP/pseudocode example as per my comment:

//nicer way to get the id of the user you just inserted!
$id = mysql_insert_id();

$sql = "SELECT `extension` FROM users ORDER BY `extension` ASC";
$res = mysql_query($sql);

$i=0;
while($n = mysql_fetch_array($res)){
  if($i==0){
    $i=$n['extension'];
  }
  if($i==$n['extension']){
    $i++;
  } else {
    break;
  }
}

//No existing users, start at 100
if($i==0){
  $i=100;
}

Then use $i as your extension.

Upvotes: 1

Related Questions