我正在使用codeigniter爲我的網站構建一個私人消息應用程序。一切正常,但我有一個取回收件箱對話的主要問題。當用戶A向用戶B發送消息時,用戶B能夠接收該消息;它會提取消息詳細信息和用戶的詳細信息,包括頭像和用戶名,但是當USER C也向USER B發送消息時。每條消息(包括用戶A的詳細信息,如頭像和名稱(消息和主題都不會))會更改爲用戶C,使其成爲原始消息的發件人。我不知道什麼是錯的。以下是我的數據庫結構和我的代碼。codeigniter消息應用程序收件箱數據庫提取問題
conversation table
----------------------------------------------------
conversation_id | conversation_subject
____________________________________________________
18 | hello there.
19 | Chelsea is winning tomorrow
___________________________________________________
conversations members table
---------------------------------------------------------------------------
conversation_id | user_id | conversation_last_view | conversation_deleted
___________________________________________________________________________
18 | 1 | 0 | 0
18 | 11 | 0 | 0
19 | 1 | 0 | 0
19 | 13 | 0 | 0
___________________________________________________________________________
conversations messages
----------------------------------------------------------------------------------------------------------------------
message_id |conversation_id | user_id | message_date | message_text
______________________________________________________________________________________________________________________
18 | 1 | 1 | 2016-05-03 20:06:53 | I just want you to know that , you shaa
18 | 11 | 13 | 2016-05-03 20:23:53 | hello guy
_______________________________________________________________________________________________________________________
控制器=
public function inbox() {
$user['user'] = $this->data;
// store's logged in user's id
$user_id = $user['user'][0]->id;
$taxi['conversation'] = $this->conversation_model->fetch_messages($user_id);
if (!empty($taxi['conversation'])) {
$conversation_id = $taxi['conversation'] [0]['conversation_id'];
$conversation['inbox'] = $this->conversation_model->fetch_inbox($conversation_id,$user_id);
//fetches member of the conversation
$member_id = $taxi2['inbox'] [0]-> user_id;
// Fetching message member that will later appear as the sender on the inbox page.
$message_member['sender'] = $this->conversation_model->fetch_member($member_id);
// merge all the data
$inbox = array_merge($user,$conversation,$message_member);
$this->load->view('template/inbox',$inbox);
}
else {
$this->load->view('template/no_message');
}
}
MODEL
public function fetch_member($member_id)
{
$q = $this->db->get_where('gamers', array('id' => $member_id));
if ($q->num_rows > 0) {
return $q->result();
}
return false;
}
public function fetch_inbox($conversation_id,$sender_id)
{
$query = $this->db->query("select conversations_members.conversation_id,conversations_members.user_id,conversations_members.conversation_last_view,\n"
. "conversations_members.conversation_deleted,conversations_messages.message_id,conversations_messages.message_date,conversations_messages.message_text\n"
. "\n"
. "from conversations_members\n"
. "\n"
. "\n"
. "inner join conversations on conversations_members.conversation_id = conversations.conversation_id
inner join conversations_messages on conversations.conversation_id = conversations_members.conversation_id
where conversations.conversation_id = $conversation_id and conversations_members.conversation_deleted = 0 and conversations_members.user_id <> $sender_id");
$results= $query->result();
return $results;
}
public function fetch_messages($sender_id)
{
$this->db->select('conversations.conversation_id,conversations.conversation_subject,conversations_messages.message_text');
$this->db->select_max('conversations_messages.message_date', 'conversation_last_reply');
$this->db->select_max('conversations_messages.message_date > conversations_members.conversation_last_view', 'conversation_unread');
$this->db->from('conversations');
$this->db->join('conversations_messages','conversations.conversation_id=conversations_messages.conversation_id','left');
$this->db->join('conversations_members','conversations.conversation_id= conversations_members.conversation_id','inner');
$this->db->where('conversations_members.user_id',$sender_id);
$this->db->where('conversations_members.conversation_deleted','0');
$this->db->group_by('conversations.conversation_id');
$this->db->order_by('conversation_last_reply', 'desc');
// Get the results from db
$query = $this->db->get();
// Result array contains the records selected
$result_array = $query->result_array();
return $result_array;
}
更新:我認爲存在的問題來源於此部分
$taxi['conversation'] = $this->conversation_model->fetch_messages($user_id);
if (!empty($taxi['conversation'])) {
$conversation_id = $taxi['conversation'] [0]['conversation_id'];
它返回的會話細節,如消息數組,文字和會話成員的ID。像下面
Array ([conversation] => Array ([0] => Array ([conversation_id] => 25 [conversation_subject] => helloooooooo guy [message_text] => hello [conversation_last_reply] => 2016-05-08 02:59:06 [conversation_unread] => 1) [1] => Array ([conversation_id] => 23 [conversation_subject] => this is new [message_text] => helllo [conversation_last_reply] => 2016-05-03 20:06:53 [conversation_unread] => 1)))
我想從數組中挑選每個conversation_id。但想象一下,如果有400個對話要顯示。我如何從數組中獲取所有conversation_id,然後將它們存儲到另一個數組中,以便我可以使用它從400條消息中獲取每個會話成員?聽起來對我來說是不可能的。有沒有另一種方法可以讓這個簡單?