jwk811 Posted February 22, 2010 Share Posted February 22, 2010 $i = 0; while($message_array = db_fetch_array($message_query) && $i < 10) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>'; $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; $i++ } why isnt this working. everything works until i add in the $i = 0 and the && $i < 10. im trying to limit it Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 What does "isn't working" mean? Are you getting errors? What is it doing or not doing that you expect it to? However, I have to ask, why are you using a variable to only display 10 records? just modify your query to ONLY return the number of records that you want. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 $sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time" . " FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last; $message_query = db_query($sql); //Loop through each message and create an XML message node for each. $i = 0; while($message_array = db_fetch_array($message_query)) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>'; $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; } if i limit 10. it will keep looping by 10s until its all displayed. maybe theres a way to get that to work? and when i say its not working i mean nothing comes up Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 if i limit 10. it will keep looping by 10s until its all displayed. Huh? That makes no sense. If you limit your query to 10 results then you will only get 10 results. It appears that is what you are trying to do. Also, are you 100% sure your query is returning results. Sometimes I make minor edits in things while I am working on soemthing else and scratch my head why something no longer works. You are apparently using some custome functions for the database actions, use whatever one you have to echo the number of results to the page and try this: $sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > {$last} LIMIT 0, 10"; $message_query = db_query($sql); //Loop through each message and create an XML message node for each. while($message_array = db_fetch_array($message_query)) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>'; $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; } Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 i used ur code and its still writting out 10 rows then takes a second writes out another 10 then does it again until theres all 100 and what not lol ughhh. thanks for trying though. hope i can figure it out or come up with something else to make it work. any other ideas would be great Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 Does this code appear inside another loop? Or is it being called by something in a loop? Or is the same code run else where? Quote Link to comment Share on other sites More sharing options...
sasa Posted February 22, 2010 Share Posted February 22, 2010 change && to and Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 Sasa's suggestion may fix the apparent issue (I was going to suggest encasing the first condition in the IF within parens), but the real issue is why you are not able to retrieve just 10 records. i used ur code and its still writting out 10 rows then takes a second writes out another 10 then does it again until theres all 100 and what not lol ughhh The fact that it shows 10 records pauses and then shows another 10 results is a significant detail. Normally, a page is not delivered to the user until the entire thing has been created (only things like images and such take a while to load). The fact that you are seeing blocks of text appear one after the other suggest that there is definitely something else going on and that there is another loop in place. But, it is apparently due to some other code that was not provided. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 <?php //Send some headers to keep the user's browser from caching the response. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-Type: text/xml; charset=utf-8"); require('database.php'); //Check to see if a message was sent. if(isset($_POST['message']) && $_POST['message'] != '') { $sql = "INSERT INTO message(chat_id, user_id, user_name, message, post_time) VALUES (" . db_input($_GET['chat']) . ", 1, '" . db_input($_POST['name']) . "', '" . db_input($_POST['message']) . "', NOW())"; db_query($sql); } //Check to see if a reset request was sent. if(isset($_POST['action']) && $_POST['action'] == 'reset') { $sql = "DELETE FROM message WHERE chat_id = " . db_input($_GET['chat']); db_query($sql); } //Create the XML response. $xml = '<?xml version="1.0" ?><root>'; //Check to ensure the user is in a chat room. if(!isset($_GET['chat'])) { $xml .='Your are not currently in a chat session. <a href="">Enter a chat session here</a>'; $xml .= '<message id="0">'; $xml .= '<user>Admin</user>'; $xml .= '<text>Your are not currently in a chat session. <a href="">Enter a chat session here</a></text>'; $xml .= '<time>' . date('h:i') . '</time>'; $xml .= '</message>'; } else { $last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0; $sql = "SELECT message_id, user_name, message, date_format(post_time, '%h:%i') as post_time FROM message WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > {$last} LIMIT 0, 10"; $message_query = db_query($sql); //Loop through each message and create an XML message node for each. while($message_array = db_fetch_array($message_query)) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>'; $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; } } $xml .= '</root>'; echo $xml; ?> thats the php code this is the javascript that leads to it function sendChatText() { if(document.getElementById('txt_message').value == '') { alert("You have not entered a message"); return; } if (sendReq.readyState == 4 || sendReq.readyState == 0) { sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true); sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); sendReq.onreadystatechange = handleSendChat; var param = 'message=' + document.getElementById('txt_message').value; param += '&name=Ryan Smith'; param += '&chat=1'; sendReq.send(param); document.getElementById('txt_message').value = ''; } } i dont think theres anything else that has to do with it. but yeah wish it would just give me 10. its a shoutbox script. i only want the last 10 messages to show Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 Is it possible that AJAX sends a request for this information at *any* other time, other then when sendChatText is called? Also, is sendChatText called more than once? If you're using FireFox, I would highly suggest you look for an addon called FireBug. You can use FireBug to insert pauses into the javascript when you load your page. FireBug will prompt you to continue the javascript whenever you reach a pause. You can use this to count the times a particular piece of javascript is being run. Also, you're using $_GET directly in SQL queries. This is not a safe thing to do as someone can easily change your $_GET values into malicious code that you then insert into SQL statements that run on your database. I always cast $_GET values that I know are integers into ints using (int)$_GET - for text, always use mysql_real_escape() or another appropriate escaping function to convert any possible SQL injections into text that won't execute. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 oh my god! yes thats why! it refreshes the function every two seconds and shows 10 posts everytime. that would make sense. now just gotta figure out a way to change that around a bit. thanks a lot, makes it a lot easier =] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 also sorry for confusing everyone lol. so now ive decided i will make a row to say if the message is old or new. and whenever i run the function make the last 10 messages only new and the rest old. thats doable right? uhhh sounds easier than it looks. any help with that please? haha Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 $sql = "UPDATE message SET status = 'old'"; $message_query = db_query($sql); $sql = "UPDATE message SET status = 'old' WHERE post_time < NOW()"; $message_query = db_query($sql); i think that will work. but right now it will show any messages before this very second. how can i change the time to like 30 minutes. any row in the database that has been there for less than 30 minutes i will change to new. maybe i should make a new post for this since its completely different now? Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 and the second query should say "new" instead of old oops Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 also sorry for confusing everyone lol. so now ive decided i will make a row to say if the message is old or new. and whenever i run the function make the last 10 messages only new and the rest old. thats doable right? uhhh sounds easier than it looks. any help with that please? haha What?! No need to change the status of the messages to old and new - simply query for the last 10 records using LIMIT. Quote Link to comment Share on other sites More sharing options...
jwk811 Posted February 22, 2010 Author Share Posted February 22, 2010 its because that function gets called every 2 seconds to refresh the chat. so it keeps putting up 10 every 2 seconds. but now i realized that even if i change it to old and new it will keep doing the same thing just putting up new ones though but over and over again. its supposed to know the last message they looked at and do something with that. i dont know Quote Link to comment Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 That will NOT work (at least, not exactly the way you intend). Because of the (fairly remote) possibility of an entry getting added between when new messages are pulled, and when new messages are marked as old, it's possible for a message not to get shown to the user but then be marked as old (even though the user has never seen it). Instead, you should get the time in PHP when you're first checking for the new messages, then use that same time to mark messages of that time or older as old. This of course assumes that your PHP and MySQL servers are the same (OR that the timestamps in MySQL are generated by the PHP server). Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 22, 2010 Share Posted February 22, 2010 OK, here is what I understand, and what I would do: You want the shoutbox to show the last 10 messages and updated every 2 seconds if there have been changes. It appears there is a flaw in the JavaScript that populates the return value from the AJAX call. Instead of replacing the current content it sounds like it is appending [need to see the function handleSendChat()]? Here is what I would do: When you do a check every two seconds you could either always grab the last ten results OR do a chek to see if there are newer records since the last call. To even to a check you would first have to do a database query, so I would have the script always pull the last 10 records. But, that doesn't mean you always have to update the content. So, when making the AJAX request include a parameter for lastRecordID. That will include the id of the most current record from the last request. On the initial request the value could be set to false. On the PHP script, do the query to get the last 10 records. Then on the very first record, check to see if the ID matches the ID passed in the AJAX request. If it matches, then simply return false. If it does not match then loop through the 10 records and build the output. The JavaScript that processes the AJAX request output would check the return value. If the value = false, then do nothing. Otherwise populate the returned results. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.