简体   繁体   English

Codeigniter消息传递应用程序收件箱数据库获取问题

[英]codeigniter messaging app inbox database fetching issue

I am building a private messaging app for my website using codeigniter. 我正在使用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; 当用户A向用户B发送消息时,用户B能够接收到该消息; it fetches the message detail and the user a details including avatar and username , but when USER C sends USER B a message too. 它获取消息详细信息和用户详细信息,包括头像和用户名,但是当USER C向USER B发送消息时也是如此。 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. 每条消息(包括用户A的详细信息(例如头像和姓名)(但消息和主题均未包含))都更改为用户C,从而使它们成为原始消息的发送者。 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. 它返回一系列对话详细信息,例如消息,文本和对话成员的ID。 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. 我想从数组中选择每个对话ID。 But imagine if there's like 400 conversations to display . 但是,假设是否要显示400个对话。 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 ? 我如何从数组中获取所有对话ID,然后将它们存储到另一个数组中,以便可以使用它从400条消息中获取每个对话成员? Sounds a bit impossible to me. 对我来说听起来有点不可能。 Is there another way i can make this simple? 还有另一种方法可以使我变得简单吗?

我通过将诸如from_user_avatar和to_user_avatar之类的列添加到消息表中来解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM