Please help me, i'm working in a chat, but only the user who sent the message, can see it. the other one cant, example:
Logged in as David:
(cant upload photos)
-ME: Hello
-ME: Lalalala
Logged in as Shinny:
-ME: Hihihi
it is supposed that what shinny writed should be displaying in my chat, and what im writing should display on shinny's chat, but the incoming message is not showing
How it should be
-ME: Hello
-Shinny: Hihihi
-ME: Lalalala
Here is the code: What can i do to solve this thing?
<?php
$chaquery = "SELECT u.*,c.* FROM chats c
INNER JOIN users u WHERE u.user_id = c.chat_from
AND chat_from = '".$session['user_id']."'
AND chat_to = '".$_GET['id']."'
(id of the destiny user)
OR u.user_id = c.chat_to AND u.user_id = c.chat_from
AND chat_to = '".$session['user_id']."'
AND chat_from = '".$_GET['id']."'
(id of the destiny user AGAIN)
ORDER BY chat_id ";
$chares = mysql_query($chaquery);
if($chares)
{
while($chafilas = mysql_fetch_assoc($chares))
{
$me = $chafilas["chat_remit"];
$message = $chafilas["chat_cont"];
$hour = $chafilas["chat_hora"];
$date = $chafilas["chat_fecha"];
$day = $chafilas["chat_dia"];
$fullname = $chafilas["usuario_nombre"];
$nick = $chafilas["usuario_nick"];
$userphoto = $chafilas["usuario_foto"];
if($me){?><div class="panel-body">
<img src="avatar/<?php echo $tufoto;?>" style="float:left; margin-right:10px;" width="55px" class="ui"/>
<a href="<?php echo $xnick;?>"><b style="font-size:16px;"><?php echo $xnombre;?></b></a><br><?php echo $xmensaje;?>
</div><?php }}}?>
Here THE TABLE
`chat_id` int(11) NOT NULL AUTO_INCREMENT,
`chat_from` varchar(45) COLLATE utf8_bin NOT NULL,
`chat_to` varchar(45) COLLATE utf8_bin NOT NULL,
`chat_message` longtext COLLATE utf8_bin NOT NULL,
`chat_hour` varchar(10) COLLATE utf8_bin NOT NULL,
`chat_day` varchar(2) COLLATE utf8_bin NOT NULL,
`chat_date` varchar(7) COLLATE utf8_bin NOT NULL,
`chat_seen` varchar(2) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`chat_id`)
there is nothing wrong with your code..are you saving these chat content somewhere?? if yes you have to refresh that particular div which contain your chat msgs using ajax call something like this....
<script type="text/javascript">
$(function() {
//populating chat the first time
refresh_chat();
// recurring refresh every 1 seconds
setInterval("refresh_chat()", 1000);
$("#send").click(function() {
// getting the values that user typed
var send = $("#send").val();
var chat = $("#chat").val();
var data = 'add='+ add + '&comp='+ comp;
$.ajax({
type: "POST",
url: "chat.php",
data: data,
success: function(html){
// this happen after we get result
$("#scroll").fadeOut(500, function(){
$(this).html(html).fadeIn(500);
$("#scroll").val("");
//alert('hello');
});
});
return false;
}
function refresh_shoutbox() {
var data = 'refresh=1';
$.ajax({
type: "POST",
url: "chat.php",
data: data,
success: function(html){ // this happen after we get result
$("#chat").html(html);
}
});
});
});
</script>
hope this helps u :)
Using UNION
, you can do 2 queries to get the results you want
SELECT * FROM (
// Get messages to
SELECT u1.* ,c1.* FROM chats c1
INNER JOIN users u1
ON u1.user_id = '".$session['user_id']."'
AND c1.chat_to = '".$session['user_id']."'
AND c1.chat_from = '".$_GET['id']."'
UNION
// Get messages from user
SELECT u2.* ,c2.* FROM chats c2
INNER JOIN users u2
ON u2.user_id = '".$_GET['id']."'
AND c2.chat_to = '".$_GET['id']."'
AND c2.chat_from = '".$session['user_id']."'
) result
ORDER BY chat_id
SQLFiddle Example - http://sqlfiddle.com/#!2/3d2af7/29
Note,
(1) you are open to sql injection by using $_GET
values directly in your query, and
(2) from the mysql_
doc - This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.