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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.