Jump to content

Retrieving Chat messages between two users with Codeigniter


Youngn

Recommended Posts

Hi

I implemented a chat system using Codeigniter where users can send and receive chat messages but the problem I'm having is that everyone can see everyone's chat.

How can I make the chat messages only accessible to the sender and receiver/recipient and vice versa?

 

Controller - Chat.php

public function ajax_get_chat_messages(){
$chat_id = $this->input->post('chat_id');
$recipient = $this->input->post('recipient');

if (!$recipient){
echo $this->_get_chat_messages($chat_id);
}
}

function _get_chat_messages($chat_id){

$last_chat_message_id = (int)$this->session->userdata('last_chat_message_id_' . $chat_id);

$chat_messages = $this->Chat_model->get_chat_messages($chat_id, $last_chat_message_id);

if ($chat_messages->num_rows() > 0)
{

//store last chat message id
$last_chat_message_id = $chat_messages->row($chat_messages->num_rows() - 1)->chat_message_id;
$this->session->set_userdata('last_chat_message_id_' . $chat_id, $last_chat_message_id);
$chat_messages_html = '<ul>';
foreach($chat_messages->result() as $chat_message)
{

$li_class = ($this->session->userdata('user_id') == $chat_message->user_id) ? 'class="by_current_user"' : '';

$chat_messages_html .='<li ' . $li_class. '>' . '<span class="chat_message_header">' . $chat_message->chat_message_timestamp . ' by ' . $chat_message->username . '</span><p class="message_content">' .$chat_message->chat_message_content . '</p></li>';
}

$chat_messages_html .='</ul>';



$result = array('status' =>'ok', 'content'=>$chat_messages_html);


return json_encode($result);

}
else
{
$result = array('status' =>'ok', 'content'=>'');
//print_r($result);

return json_encode($result);

exit();

}


}

Model - Chat_model.php

public function get_chat_messages($chat_id, $last_chat_message_id = 0){


$query_str = "SELECT cm.chat_message_id, cm.user_id, cm.chat_message_content, DATE_FORMAT(cm.date_created, '%D of %M %Y at %H:%i:%s') AS chat_message_timestamp, u.username FROM chat_messages cm JOIN users u ON cm.user_id = u.user_id WHERE cm.chat_id = ? and cm.chat_message_id > ? ORDER BY cm.chat_message_id ASC";

$result = $this->db->query($query_str, array($chat_id, $last_chat_message_id));

return $result;
}

js - chat.js

function get_chat_messages()
{

$.post(base_url +"user/chat/ajax_get_chat_messages", { chat_id : chat_id}, function(data) {

if (data.status == 'ok')
{

var current_content = $("div#chat_viewport").html();


$("div#chat_viewport").html(current_content + data.content);

}
else
{
//there was an error do something 

}

}, "json");

}

get_chat_messages();
Link to comment
Share on other sites

What is cm.chat_id for in your table?

 

The simple answer is that you need to add to the WHERE clause in your query.  Rather than find new messages since the last one (they saw?)  you need to get new message since the last one they saw AND was sent to them, OR that they sent.  

 

With that said, it is unclear from your query what your chat table structure looks like.  The sender/receiver column names are not clear, and it's not clear what chat_id does or why you are passing it as a parameter and then using it in the where clause.

Link to comment
Share on other sites

@gizmola, Please forgive my ignorance.

 

chat_id is a hard coded column that always has a value of 1.

 

Please see screenshot of my table attached below.

 

post-205630-0-47474800-1509995288_thumb.png

 

Thank you for your feed back, so observing my query you are advising I remove the cm.chat_id from the where clause?

 

I tried using the Where in() function; but it is still seen by every user :confused:

 

.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.