【发布时间】:2011-05-10 07:45:14
【问题描述】:
我需要为每个用户生成一个唯一的扩展名(这与 ID 是分开的,但工作方式大致相同)该数字应该是 100 或更大,并且可以被覆盖/自定义设置。现在我正在使用下一个 id,如果它小于 100,则添加 100。
因此,如果下一个 id 为 5,则数字为 105,但下一个 id 为 105,则数字仅为 105。问题在于,如果用户之前选择 105,我会让用户选择他们自己的分机在这种情况下,我想让它自动跳到下一个数字 106。现在如果有 105、106 和 108,我想跳到 107,然后跳到 109。这是我用来生成数字的代码.我认为问题出在我的while循环上。我不知道如何让它继续检查唯一的数字。
这是代码,我敢肯定我的事情过于复杂了很多。
$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++;
}
【问题讨论】:
-
也许获取所有扩展名的数组,按升序排序并迭代直到找到未使用的数字会起作用吗?我会忘记 100+n 部分的事情;一旦你拥有超过 100 个用户,它就会变得完全多余。