dsdvr06648 2011-06-29 19:23
浏览 13
已采纳

php循环问题

Pardon the framework specific code. Please just take my word that the objects do what I say they do.

My Code

       //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                            
       }

My Question

What do I write in the else statement to run through the code above until I find an alias that isn't being used in the Person table. My guess is some kind of loop, but I'm just not really sure how to do it. Feel free to re-work this how you like. Put it as its own function/tell me I'm doing it wrong, I won't be offended. Thank you SO!

  • 写回答

4条回答 默认 最新

  • doumo2501 2011-06-29 19:26
    关注
    $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*/ }
    

    However, it may be a better idea to use an auto-incremented value for the alias-id if it does not have to be randomly generated.

    To convert the number to and from base36 you can use PHP's base_convert function:

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

    output:

    12345 --> 9ix  --> 12345
    

    If you want to make sure that your number is at least 5 digits start your increment value at: base_convert(10000,36,10) = 1679616

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了