I am building a private messaging app for my website using codeigniter. Everything works but i have a major problem with fetching inbox conversations. When USER A sends message to USER B. User B is able to receive the message; it fetches the message detail and the user a details including avatar and username , but when USER C sends USER B a message too. Every message (including user A's details like Avatar and name (message and subject doesnt though)) changes to User C making them the sender of the original message. I am not sure whats wrong. Below is my database structure and my codes.
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
_______________________________________________________________________________________________________________________
controller =
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;
}
UPDATE : i think the problems comes from this part
$taxi['conversation'] = $this->conversation_model->fetch_messages($user_id);
if (!empty($taxi['conversation'])) {
$conversation_id = $taxi['conversation'] [0]['conversation_id'];
it returns an array of conversation details like messages , text and conversation member's ids. like below
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 ) ) )
I am suppose to pick each and every conversation_id from the arrays. But imagine if there's like 400 conversations to display . how do i fetch all the conversation_id's from the array and then store them into another array so i can use it to fetch each conversation member from the 400 messages ? Sounds a bit impossible to me. Is there another way i can make this simple?
我通过将诸如from_user_avatar和to_user_avatar之类的列添加到消息表中来解决了我的问题。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.