我需要为每个用户生成一个唯一的扩展(这与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++;
}
答案 0 :(得分:2)
我想出了这个,没有彻底测试过,但我认为应该正确地返回下一个可用值
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
因此,如果按预期工作,您根本不需要最后几行代码。
编辑: 修改了查询,因为我意识到我从错误的一方接近它。
答案 1 :(得分:1)
根据我的评论,伪劣尝试PHP /伪代码示例:
//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;
}
然后使用$ i作为扩展程序。
答案 2 :(得分:1)
好的,所以你需要下一个可用的扩展名,而不是数据库中已有的给定数字。 因此,理想情况下,您希望数据库中的数组具有比递增排序的给定键更高的所有可用扩展。然后从给定数字循环增加1,直到它不匹配。您没有提到最大延伸次数。我会这样做:
<?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.
?>