2013-12-19 46 views
-4

下面是我試圖運行的代碼。現在我已經嘗試過各種方法來實現它的功能。我想我知道這個問題,但經過Google搜索和四處尋找,我找不到解決問題的辦法。mysqli和php if語句定義爲

目前,它將做既兩個都定義,但我需要做一個或另一個方式。這是一個從不同的網站數據庫獲取用戶的插件。

if (isset($tables['users'])) 
    if(defined('CUSTOM_USER_TABLE')){ 
     $tables['users'] = CUSTOM_USER_TABLE; 
}if(defined('CUSTOM_USER_TABLE_MALE')){ 
     $tables['users'] = CUSTOM_USER_TABLE_MALE; 
} 


    if (isset($tables['usermeta'])) 

     if(defined('CUSTOM_USER_META_TABLE')){ 
      $tables['users'] = CUSTOM_USER_META_TABLE; 
     } if(defined('CUSTOM_USER_META_TABLE_MALE')){ 
      $tables['users'] = CUSTOM_USER_META_TABLE_MALE; 
} 
+1

究竟什麼是您所遇到的問題?從我看到你正在檢查是否定義了一個特定的常量,如果是,你將它的值賦給'$ tables ['users']'。 – Crackertastic

+0

我把其他人放在其他地方,如果錯誤,我已採取其他出 –

+1

這聽起來像你要求一個簡單的'if..else'語句。我不知道你有什麼問題使用它。 – deceze

回答

1

只要使用正確的格式和一些括號,你看是怎麼回事。我猜你應該改變if (isset($tables['usermeta']))else if (isset($tables['usermeta']))

if (isset($tables['users'])) 
{ 
    if(defined('CUSTOM_USER_TABLE')) 
    { 
    $tables['users'] = CUSTOM_USER_TABLE; 
    } 
    else if(defined('CUSTOM_USER_TABLE_MALE')) 
    { 
    $tables['users'] = CUSTOM_USER_TABLE_MALE; 
    } 
} 

//using if the code below is always evaluated 
//using else if the code is only evaluated if $tables['users'] is not set 

if (isset($tables['usermeta'])) 
{ 
    if(defined('CUSTOM_USER_META_TABLE')) 
    { 
    $tables['users'] = CUSTOM_USER_META_TABLE; 
    } 
    else if (defined('CUSTOM_USER_META_TABLE_MALE')) 
    { 
    $tables['users'] = CUSTOM_USER_META_TABLE_MALE; 
    } 
} 
+0

此代碼將只運行一個,而不是兩個..這隻能從女性數據庫中提取數據 –

+2

然後我不知道你想要做什麼。您正在使用此代碼更改單個數組鍵'$ tables ['users']'。所以最後你只能得到一個結果。如果你想運行兩個表,你需要在'$ tables'數組中至少有兩個鍵。假設您稍後在該陣列上運行一些代碼 –