php循环问题

时间:2011-06-29 19:23:14

标签: php mysql loops

请原谅框架特定的代码。请接受我的说法,即对象按我说的做。

我的代码

       //Generates a random five digit alphanumerical id
       $aliasId = $model->random_id_gen('5');

       //calls the active record class for table Person                     
       $person = new Person();

       //searches the person table to see if this alias is being used         
       $search = $person->find('alias=:alias', array(':alias'=>$aliasId));

       //if null then it sets an attribute for a another active record class                     
       if ($search==NULL)
       {
               $model->setAttribute('alias', $aliasId);
               $model->setIsNewRecord(TRUE);
       }
       else
       {
       //I need to loop through the above code until I find an alias that isn't being used                            
       }

我的问题

我在else语句中编写什么来运行上面的代码,直到找到Person表中没有使用的别名。我的猜测是某种循环,但我只是不确定如何做到这一点。随意重新按照自己喜欢的方式工作。把它作为自己的功能/告诉我,我做错了,我不会被冒犯。谢谢你!

4 个答案:

答案 0 :(得分:1)

$found = false;
$iter = 0;        // It's a good idea to include an upper bound on the number of iterations
while(!$found && $iter < 1000){
   $aliasId = $model->random_id_gen('5');

   //calls the active record class for table Person                     
   $person = new Person();

   //searches the person table to see if this alias is being used         
   $search = $person->find('alias=:alias', array(':alias'=>$aliasId));

   //if null then it sets an attribute for a another active record class                     
   if (is_null($search)){
       $model->setAttribute('alias', $aliasId);
       $model->setIsNewRecord(TRUE);
       $found = true;
   }
   $iter++;
}

if(!$found){ /* Some error condition because a suitable ID could not be found*/ }

但是,如果不必随机生成alias-id,则使用自动递增的值可能更好。

要将数字转换为base36,您可以使用PHP的base_convert函数:

$a = base_convert(12345,10,36);
$b = base_convert($a,36,10);
print "12345 --> ".$a."  --> ".$b;

输出:

12345 --> 9ix  --> 12345

如果您想确保您的号码至少为5位数,请在以下位置开始您的增量值:base_convert(10000,36,10) = 1679616

答案 1 :(得分:0)

这个怎么样:

$search = true; //set to a non-null value
while (!is_null($search)) {
    $aliasId = $model->random_id_gen('5');

    //calls the active record class for table Person                     
    $person = new Person();

    //searches the person table to see if this alias is being used         
    $search = $person->find('alias=:alias', array(':alias'=>$aliasId));
}
$model->setAttribute('alias', $aliasId);
$model->setIsNewRecord(TRUE);

答案 2 :(得分:0)

我会用一段时间循环......

//calls the active record class for table Person                     
$person = new Person();

//Generates a random five digit alphanumerical id
$aliasId = $model->random_id_gen('5');

while ($person->find('alias=:alias', array(':alias'=>$aliasId)))
{
    $aliasId = $model->random_id_gen('5');
}
//sets an attribute for a another active record class                     
$model->setAttribute('alias', $aliasId);
$model->setIsNewRecord(TRUE);

我真的会研究一种更好的方法来生成aliasId值,因为一旦你拥有了很多“人”记录,你就会循环很长时间。

答案 3 :(得分:0)

我的版本具有最大迭代设置(如果要让它运行直到找到唯一的设置,则设置为-1)

$person = new Person();
$unique = false;
$maxloops = 100;
while($maxloops>=0){
    //Generates a random five digit alphanumerical id
    $aliasId = $model->random_id_gen('5');
    //searches the person table to see if this alias is being used
    $unique = empty($person->find('alias=:alias', array(':alias'=>$aliasId)));         
    if($unique) break;
    $maxloops--;
}

if($unique){
    $model->setAttribute('alias', $aliasId);
    $model->setIsNewRecord(TRUE);
}else{
    trigger_error("oops!");
}