2012-12-15 89 views
1

我有點困惑,關於爆發和繼續循環等我有2個SQL查詢匹配用戶priveleges與新的用戶的實際priveleges把它。然而,如果一些新priveleges的匹配一個用戶擁有,我想跳過SQL插入並移動到下一個:PHP中斷,繼續在2循環

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'語句,但不知道在哪裏。

+0

你用'$ X做些什麼= false;'? – alfasin

+0

不管那是以前的功能。這個沒用。 LOL – Dimitri

+2

'繼續2'應該做的伎倆,它跳過你的'foreach'的頂部。 –

回答

3

INSERT語句之後,可以添加continue 2帶給你回到你的foreach ($ex as ...的頂部。在這種情況下,您也可以使用break;,因爲您的內心沒有任何東西while

但是,如果您以不同方式進行操作,實際上並不需要它。而不是閱讀每個特權的表,只讀一遍,然後比較。

此代碼將從數據庫獲取所有權限,然後僅根據$ex插入缺少的權限;它使用array_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); 
     } 
    } 
} 

順便說一句,你可以考慮使用INSERT IGNORE INTO因爲比賽條件,但因爲你沒有檢查語句返回值,都不會有問題在這裏:)

+0

+1此解決方案將所有對數據庫的SELECT調用減少到只有一個調用,並且所有INSERT調用都只是一個調用。這個解決方案的附加價值是BRILLIANT! – alfasin

+0

@alfasin那麼,實際上可以有多個INSERT語句:) –

+0

嗯,在這種情況下,我相信它可以被縮減爲一個查詢(使用字符串附加),如下所示:http://www.electrictoolbox。com/mysql-insert-multiple-records/ – alfasin

1

的INSERT後,只需添加一休:

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

是完整的我會推薦以下鏈接閱讀: http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated

+0

+1這是一個很好的解決方案 – alfasin