Jump to content

Ajax chat help


mike1313

Recommended Posts

Well with the following code I can't seem to limit the amount of messages displayed. I'm think maybe the last 15 messages but I can't get it to work. Any help?

 

$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time 
	 FROM message WHERE message_id > $last";
$message_query = db_query($sql);


        

        
if(db_num_rows($message_query) > 0) {
	$json .= '"message":[ ';	
	while($message_array = db_fetch_array($message_query)) {
                        
                        
		$json .= '{';
		$json .= '"id":  "' . $message_array['message_id'] . '",
					"user": "' . $message_array['user_name'] . '",

                                                "text": "' . $message_array['message']  . '",
                                              
					"time": "' . $message_array['post_time'] . '"
				},';

 

I've tried using ORDER BY in the sql statement also and that didn't work. Like it I used

$sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time 
	 FROM message WHERE message_id > $last ORDER BY id ASC LIMIT 1";

 

It starts off displaying the message then another message appears everytime it refreshes and its just a continuous cycle. Any help or thoughts on how I can get this working the correct way is greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/79057-ajax-chat-help/
Share on other sites

I think so.

 

$json .= '{';
		$json .= '"id":  "' . $message_array['message_id'] . '",
					"user": "' . $message_array['user_name'] . '",

                                                "text": "' . $message_array['message']. '",
                                              
					"time": "' . $message_array['post_time'] . '"
				},';
	}
	$json .= ']';
} else {
	//Send an empty message to avoid a Javascript error when we check for message length in the loop.
	$json .= '"message":[]';
}
}
//Close our response
$json .= '}}';
echo $json;

 

I really don't know why it won't work because without limiting the query to the database is bogging down my server.

Link to comment
https://forums.phpfreaks.com/topic/79057-ajax-chat-help/#findComment-402886
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.