2015-04-15 76 views
0

我試圖實現多個if條件來檢查提案的狀態,並且只應執行特定代碼(如果狀態代碼設置爲1或5)。PHP IF/ELSE條件不能按預期工作

由於某種原因,我在實施這方面遇到了困難。目前代碼中的邏輯是,如果提案狀態不匹配1或5,則返回消息,否則執行下一個查詢。當我只指定一個數字,即(1或5)時,它會正常工作。

我與若和其他條件,在這部分面臨着另一個問題:

if ($count == 1) { 

     $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    } 

    if ($count < 1) { 

     $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal"); 

     $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR); 
     $status->bindParam(':proposal', $proposal, PDO::PARAM_STR); 
     $status->execute(); 

     $proposalstatus = $status->fetchColumn(); 

     if($proposalstatus != 1) 
     { 
       //echo $proposalstatus; 
      $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>'; 
     } 
    } 

    else { 

當我單獨運行每個部分,但是當我試圖把它一起在if語句中失敗,並沒有按這個工程根本不檢查這些條件,只是完成更新數據庫並顯示成功消息的任務。

完整的代碼是在這裏:

try 
    { 
    $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password); 

    $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

    $username = $_SESSION['username']; 

    $sql = $db_conx->prepare("SELECT username, user_record_id FROM login_details 
     WHERE username = :username"); 

    $sql->bindParam(':username', $username, PDO::PARAM_STR); 

    $sql->execute(); 
    $user_record_id = $sql->fetchColumn(1); 

    $proposal = $_POST['proposal_id']; 

    $acceptCheck = $db_conx->prepare ("SELECT * FROM record WHERE student_record_id = :user_record_id AND status_code = 3"); 
    $acceptCheck->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR); 
    $acceptCheck->execute(); 

    $count = $acceptCheck->rowCount(); 

    if ($count == 1) { 

     $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    } 

    if ($count < 1) { 

     $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal"); 

     $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR); 
     $status->bindParam(':proposal', $proposal, PDO::PARAM_STR); 
     $status->execute(); 

     $proposalstatus = $status->fetchColumn(); 

     if($proposalstatus != 1 || 5) //status must be either 'Approved' code 1 or 'Held' code 5 
     { 
       //echo $proposalstatus; 
      $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>'; 
     } 
    } 

    else { 

       //Update all application records to 'Not available' when a proposal has been accepted 

     $updateOtherRecords = $db_conx->prepare("UPDATE record SET status_code = 8, last_updated = now() 
      WHERE proposal_id = :proposal"); 
     $updateOtherRecords->bindParam(':proposal', $proposal, PDO::PARAM_STR); 
     $updateOtherRecords->execute(); 

       //Update other applicationa for the user concerned to 'Rejected' 

     $updateUserRecord = $db_conx->prepare("UPDATE record SET status_code = 7, last_updated = now() 
      WHERE student_record_id = :user_record_id"); 
     $updateUserRecord->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR); 
     $updateUserRecord->execute(); 

       //Update the proposal concerned and assign it to the user 

     $update = $db_conx->prepare("UPDATE record SET status_code = 3, last_updated = now() 
      WHERE proposal_id = :proposal AND student_record_id = :user_record_id"); 
     $update->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR); 
     $update->bindParam(':proposal', $proposal, PDO::PARAM_STR); 
     $update->execute(); 

     $feedback = '<p class="text-success"> The proposal has been successfully accepted <span class="glyphicon glyphicon-ok"/></p>'; 
    } 
} 

我真的需要知道我怎麼可以排序,因爲我將使用if和else很多這種說法。任何指導將非常感謝!

預先感謝您!

+4

表達式$ proposalstatus!= 1 || 5'並沒有做到你的意思。你必須完全表達這兩個條件,所以它看起來像'if($ proposalstatus!= 1 && $ proposalstatus!= 5)',這意味着將其改爲'&&'使邏輯工作。 –

+0

'vardump($ proposalstatus);',你應該使用。 – Hackerman

+0

print_r($ proposalstatus)也很有用 –

回答

2

你的條件不

if ($count < 1) { 
    some stuff 
} 

if ($count == 1) { 
... 
} else 
... this code will execute when $count is *NOT* equal to 1, 
    which includes when it's LESS than 1, e.g. "< 1" is true here 
} 

也許你想

if ($count == 1) { 
} else if ($count < 1) { 
} else { 
} 

讓別人最終如果/當$count >= 1

+0

感謝您的回覆。基本上,我只是想檢查計數是否爲1,然後是否返回第一條消息,如果不是則執行第二部分。所以你說我應該這樣做,如果條件? – user610

+0

好吧,考慮一下你的代碼有第1部分(第一個if),然後是第2A部分和第2B部分。按照書面的說法,如果'$ count'確實是1,那麼你會執行第1部分和第2B部分。不知道這是否是你想要的。 –

+0

我有點困惑。第1部分將檢查計數== 1,如果它不是,那麼第2A部分將計數<1,然後檢查提議的狀態,如果這一切都好,那麼我只是希望它從那裏繼續。我覺得我自己很困惑,因爲我有很多嵌套的if語句來檢查各種東西 – user610

1

與提案狀態替換你的病情纔會運行互斥1或5

if(!($proposalstatus == 1 || $proposalstatus == 5)) { 
$feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>'; 
} 
+0

感謝您的意見。這個解決方案和@MichaelBerkowski提供的解決方案都是完美的:-) – user610

+0

歡迎您:-) –