2017-05-26 40 views
0

我試圖找到用戶是否發送了一個朋友請求或不。如果用戶沒有發送任何請求,我想顯示發送請求按鈕。做一個檢查,如果ID不存在做某事

所以我有一個表叫朋友

id 
user_id 
friend_id 
reqs_status which can be 0,1,2 

reqs_status = 0 meaning request already sent and waiting for approval 
reqs_status = 1 meaning show Accept or Reject button 
reqs_status = 2 meaning they are friends already 
No row existing meaning not sent any request [show send request button] 

If user A sends request to B. I create 2 rows one saying 
user_id 1 sent a request to friend_id 2 with reqs_status as 0 and 
another as inverse user_id 2 got request from friend_id 1 with reqs_status as 1 

這裏reqs_status在顯示在屏幕上向右按鈕發揮關鍵作用。這樣的:已經採取用戶,好友請求發送,發送的請求,接受/拒絕按鈕等。

現在我的數據看起來類似:

[ 
    { 
     "id":9, 
     "firstname":"Hattie", 
     "lastname":"Cropper", 
     "email":"[email protected]", 
     "role":1, 
     "friends":[ 
     { 
      "id":7, 
      "user_id":8, 
      "friend_id":9, 
      "reqs_status":2, 

     } 
     ] 
    }, 
    { 
     "id":21, 
     "firstname":"Jon", 
     "lastname":"Olson", 
     "email":"[email protected]", 
     "role":1, 
     "friends":[ 
     { 
      "id":19, 
      "user_id":20, 
      "friend_id":21, 
      "reqs_status":2, 

     }, 
     { 
      "id":29, 
      "user_id":23, 
      "friend_id":21, 
      "reqs_status":1, 

     } 
     ] 
    }, 
    { 
     "id":24, 
     "firstname":"Jessica", 
     "lastname":"Jensen", 
     "email":"[email protected]", 
     "role":1, 
     "friends":[ 
     { 
      "id":24, 
      "user_id":25, 
      "friend_id":24, 
      "reqs_status":2, 

     }, 
     { 
      "id":30, 
      "user_id":23, 
      "friend_id":24, 
      "reqs_status":0, 
     } 
     ] 
    } 
] 

如果沒有發送意味着要求有中沒有數據朋友表。

@foreach ($user['friends'] as $friend) 
@if($friend['reqs_status'] == 0 && $friend['user_id'] == $myid) 
//show button already sent 
@elseif($friend['reqs_status'] == 1 && $friend['user_id'] == $myid) 
//show accept reject button 
@elseif ($friend['user_id']== '') 
//show send request button 
@endif 
@endforeach 

因爲有朋友和每個那些其他朋友陣列會有button.Instead我想用ELSEIF 檢查,如果USER_ID不以朋友數組列表存在,那麼我不能做@else顯示發送請求按鈕。如果我做$朋友['user_id']!= $ myid,這意味着如果一個用戶有10個朋友,並在沒有我的ID存在,它會添加10個按鈕,而不是添加單個按鈕我需要一個單一的行動,

以上條件:@elseif ($friend['user_id']== '')失敗。我不知道如何比較一些不存在的東西:/

我希望你能理解這個問題。如果不清楚,請在評論中告知我,我會盡量在聊天中儘可能與您清楚。

如果在數組對象中發現匹配($ myid),它應該忽略數組中的其他對象,某種中斷或跳過或繼續,但如果沒有在整個陣列中找到匹配做X的事情,這將是創建一個單一的按鈕。

回答

0

嘿,你可以使用這段代碼,它會解決你的問題!

@foreach ($user['friends'] as $friend) 
 
@if($friend['user_id'] == $myid) 
 
@switch ($friend['reqs_status']) 
 
case 0: 
 
//show button already request sent 
 
break; 
 
case 1: 
 
//show accept and reject request button 
 
break; 
 
case 2: 
 
//meaning they are friends already 
 
default: 
 
# code... 
 
break; 
 
@endswitch 
 
@elseif(empty($friends['user_id'])) 
 
//show send request button 
 
@endif 
 
@endforeach

請讓我知道如果您有任何疑問!

+0

我用你的代碼建議,但開關案例代碼似乎在屏幕上打印和刀片引擎似乎並沒有抓住 –

相關問題