2016-11-28 94 views
1

我期望從foreach輸出中排除特定值,其輸出發生在friend_onefriend_two。我不想從我的查詢中排除這個。從foreach循環輸出中排除值

排除它在任一friend_onefriend_two發生將是$profile_user參數。我試圖做到這一點if(in_array($friend_total_row->friend_one == $profile_user)),但我得到它的錯誤,再加上它只有friend_one在它。

任何人都有一個想法,我可以做到這一點?

<?php   
    //Friends --- total 
    $friend_status = 2; 
    $friend_sql = " 
     SELECT * 
     FROM friends 
     WHERE (friend_one = :profile_user or friend_two = :profile_user) 
     AND status = :total_status 
    "; 
    $friend_stmt = $con->prepare($friend_sql); 
    $friend_stmt->execute(array(':profile_user' => $profile_user, ':total_status' => $friend_status)); 
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC); 
    $count_total_friend = $friend_stmt->rowCount(); 
?> 
     <div id="friend-list-container"> 
      <div id="friend-list-count">Friends <span class="light-gray"><?php echo $count_total_friend; ?></span></div> 
      <div id="friend-list-image-container"> 
<?php   
    foreach ($friend_total_rows as $friend_total_row) { 
     if(in_array($friend_total_row->friend_one == $profile_user)) { 
      continue; 
     } 
     else { 
     $friend_1 = $friend_total_row['friend_one']; 
     $friend_2 = $friend_total_row['friend_two']; 
     //$friend_status  = $friend_total_row['status']; 
     //$friend_status_date = $friend_total_row['date']; 
     } 
     echo $friend_1 . $friend_2; 
    } 
?> 

我只是想從輸出,這是其中X是排除$profile_user。然後我想要顯示平方輸出。 enter image description here

+1

此行'如果(in_array($ friend_total_row-> friend_one == $ profile_user))'是錯誤的。你必須用逗號分隔,而不是兩個等號。 http://php.net/manual/en/function.in-array.php – 2016-11-28 20:13:26

+0

只是問爲什麼你不想在查詢中排除這個。另外,在目前的設置下,你會得到好友所描述的實例上的重複行,這是你期望的嗎? – nerdlyist

+0

@nerdlyist我不想從查詢中排除,因爲查詢是匹配關係。 IE:如果個人資料用戶與另一個用戶在同一記錄中,那就是友誼。在這個輸出中,我想排除個人資料用戶只顯示他們的朋友。 – Paul

回答

1

更新答案:

foreach ($friend_total_rows as $friend_total_row) { 
    $friend_1 = $friend_total_row['friend_one']; 
    $friend_2 = $friend_total_row['friend_two']; 

    if($friend_1 !== $profile_user) { 
     echo $friend_1; 
    } 

    if($friend_2 !== $profile_user) { 
     echo $friend_2; 
    } 
} 
+0

你給出的第一個例子是給出了' - >'的錯誤,第二個'in_array'例子給出了'注意:試圖獲取非對象的屬性'錯誤。 – Paul

+0

對,請參閱我的更新:) – Max

+0

對於數組 - 「解析錯誤:語法錯誤,意外'$ friend_total_row'(T_VARIABLE),期待']'...比較不會給出括號'[' 。 – Paul