Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中断,在2个循环中继续_Php_Loops_Foreach_Break_Continue - Fatal编程技术网

PHP中断,在2个循环中继续

PHP中断,在2个循环中继续,php,loops,foreach,break,continue,Php,Loops,Foreach,Break,Continue,我对循环的中断和继续等有点困惑。我有两个SQL查询,它们将用户权限与用户的实际权限与新权限进行匹配。但是,如果一些新的特权与用户拥有的特权匹配,我想跳过SQL插入并转到下一个: public static function insertPriveleges($user_id,$priveleges) { $ex = explode(",",$priveleges); // separated by commas if(count($ex)>0) {

我对循环的中断和继续等有点困惑。我有两个SQL查询,它们将用户权限与用户的实际权限与新权限进行匹配。但是,如果一些新的特权与用户拥有的特权匹配,我想跳过SQL插入并转到下一个:

public static function insertPriveleges($user_id,$priveleges)
{
    $ex = explode(",",$priveleges); // separated by commas
    if(count($ex)>0)
    {
         $x = false;
         foreach($ex as $i => $priv)
         {
             $check_user = mysql_query("SELECT * FROM users_access_codes WHERE user_id='$user_id'") or die(mysql_error()); // get user's current priveleges
             while($check_data = mysql_fetch_array($check_user))
             {
                  if($check_data['access_code']!=$priv)
                  {
                      //if it doesn't match, insert 
                      $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
                  }             
             }
         }
     }
}

我几乎从未遇到过需要在循环中匹配两个以上事物的情况。我需要确保我不会给那个用户带来双重特权。我知道在内部循环的某个地方必须有一个“continue”语句,但不确定在哪里。

只需在插入后添加一个中断:

public static function insertPriveleges($user_id,$priveleges)
{
    $ex = explode(",",$priveleges); // separated by commas
    if(count($ex)>0)
    {
         $x = false;
         foreach($ex as $i => $priv)
         {
             $check_user = mysql_query("SELECT * FROM users_access_codes WHERE user_id='$user_id'") or die(mysql_error()); // get user's current priveleges
             while($check_data = mysql_fetch_array($check_user))
             {
                  if($check_data['access_code']!=$priv)
                  {
                      //if it doesn't match, insert 
                      $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
                      break;
                  }             
             }
         }
     }
}
为了完整起见,我建议阅读以下链接:

只需在插入后添加一个中断:

public static function insertPriveleges($user_id,$priveleges)
{
    $ex = explode(",",$priveleges); // separated by commas
    if(count($ex)>0)
    {
         $x = false;
         foreach($ex as $i => $priv)
         {
             $check_user = mysql_query("SELECT * FROM users_access_codes WHERE user_id='$user_id'") or die(mysql_error()); // get user's current priveleges
             while($check_data = mysql_fetch_array($check_user))
             {
                  if($check_data['access_code']!=$priv)
                  {
                      //if it doesn't match, insert 
                      $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
                      break;
                  }             
             }
         }
     }
}
为了完整起见,我建议阅读以下链接:
在您的
INSERT
语句之后,您可以添加
continue 2
,使您回到
foreach($ex as…
)的顶部。在这种情况下,您还可以使用
break;
,因为在
内部while
之后没有任何内容

然而,如果你做的不同,你实际上并不需要它。与其为每个特权读取表格,只需将所有特权读取一次并进行比较

此代码将从数据库获取所有权限,然后根据
$ex
只插入缺少的权限;它使用
数组_diff()
计算两者之间的差异

public static function insertPriveleges($user_id, $priveleges)
{
    $ex = explode(",", $priveleges); // separated by commas
    if (count($ex) > 0) {
         // get user's current priveleges
         $check_user = mysql_query("SELECT * FROM users_access_codes 
             WHERE user_id='$user_id'") or die(mysql_error()); 
         $actual = array();
         while ($row = mysql_fetch_array($check_user)) {
             $actual[] = $row['access_code'];
         }

         foreach (array_diff($ex, $actual) as $priv) {
             //if it doesn't match, insert 
             $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
             mysql_query($sql);
         }
     }
}

BTW,您可以考虑使用<代码>插入忽略,因为RACE条件,但是因为您没有检查语句返回值,这里不重要:

< P>在您的代码> INSERT < /C>语句之后,您可以添加<代码>继续2 < /代码>,以使您返回到<前台的顶部。($ex as…。在这种情况下,您也可以使用
break;
,因为内部
while
之后没有任何内容

然而,如果你做的不同,你实际上并不需要它。与其为每个特权读取表格,只需将所有特权读取一次并进行比较

此代码将从数据库获取所有权限,然后根据
$ex
只插入缺少的权限;它使用
数组_diff()
计算两者之间的差异

public static function insertPriveleges($user_id, $priveleges)
{
    $ex = explode(",", $priveleges); // separated by commas
    if (count($ex) > 0) {
         // get user's current priveleges
         $check_user = mysql_query("SELECT * FROM users_access_codes 
             WHERE user_id='$user_id'") or die(mysql_error()); 
         $actual = array();
         while ($row = mysql_fetch_array($check_user)) {
             $actual[] = $row['access_code'];
         }

         foreach (array_diff($ex, $actual) as $priv) {
             //if it doesn't match, insert 
             $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
             mysql_query($sql);
         }
     }
}


BTW,您可以考虑使用<代码>插入忽略> 因为RACE条件,但是因为您没有检查语句返回值,这里就不重要了:

你怎么处理
$x=false;
?忽略这一点,这是前面的函数。这个函数没有用。LOL
continue 2
应该做到这一点,它会跳回你的
foreach
@Jack的顶部。这应该是一个答案,而不是一个注释。正确答案应该是+2:first+1,使用
c应该是+1continue 2
@cheesemacfly在本例中为否。但是,如果
while
循环的结尾和
foreach
循环的结尾之间存在任何差异,则为是:)您如何处理
$x=false?忽略这一点,这是前面的函数。这个没用。LOL
continue 2
应该可以做到,它会跳回你的
foreach
@Jack的顶部。这应该是一个答案,而不是评论。它应该得到+2:第一个+1表示正确答案,第二个+1表示使用
continue 2
@cheese。在这种情况下,如果
while
循环的结尾和
foreach
循环的结尾之间有任何差异,然后是:)+1此解决方案将把对DB的所有SELECT调用减少为一个调用,并将所有INSERT调用减少为一个调用。此解决方案的附加值非常出色@alfasin好吧,实际上可以有多个INSERT语句:)嗯,在这种情况下,我相信它可以简化为一个查询(使用字符串附加)如下:@alfasin当然,问题是这是否严格必要。事实上,准备好的陈述是我最喜欢的。对!我想这取决于您保存了多少对DB的调用;)+1此解决方案将把对DB的所有SELECT调用减少为一个调用,并将所有INSERT调用减少为一个调用。此解决方案的附加值非常出色@alfasin好吧,实际上可以有多个INSERT语句:)嗯,在这种情况下,我相信它可以简化为一个查询(使用字符串附加)如下:@alfasin当然,问题是这是否严格必要。事实上,准备好的陈述是我最喜欢的。对!我想这取决于您保存了多少对DB的调用;)