zac1987 Posted March 22, 2011 Share Posted March 22, 2011 Twitter chat features : 1) use ajax to reload the page to get new data from database for every 10 seconds, 2) When the user scroll down to the bottom of page, it load the older 5 data from database. I have 3 files (index.php, message1.php and message2.php) On index.php, I use ajax to reload the page for every 10 seconds to call a php file (message1.php). On message1.php, I use mysql to retrieve 30 records from database and php to display the records to the index.php. ($last_msg_id to hold the id of last data in message1.php, then send the $last_msg_id to index.php) When user scroll down to bottom of the page, it will auto call other php file (message2.php) to display older 5 records from database. (index.php will send $last_msg_id_id to message2.php, so that message2.php will know which was the last data previously and display the older data where id is less than $last_msg_id, eg : $query2 = "SELECT id, message, datetime FROM messages WHERE id < '$last_msg_id' LIMIT 5"; Now my problem is when the ajax reload the index.php, the value of $last_msg_id is become empty. So if the user is on the half way scrolling to bottom of page to get older data, the older data will not show up, it will show up the previous data because the value of $last_msg_id is back to empty after page is reloaded by ajax. May I know how to fix this problem please? Maybe I should not reload the page for every 10 seconds to get new data? If without reload the page, how am I going to get new data when his friends posted new messages on other side? Quote Link to comment Share on other sites More sharing options...
BluB Posted March 22, 2011 Share Posted March 22, 2011 You could make $last_msg_id a session variable, that way it'll keep it value even after refreshing. Sessions: http://php.net/manual/en/features.sessions.php Session variable tutorials: http://www.tizag.com/phpT/phpsessions.php http://www.w3schools.com/PHP/php_sessions.asp http://php.about.com/od/advancedphp/ss/php_sessions_2.htm Quote Link to comment Share on other sites More sharing options...
zac1987 Posted March 22, 2011 Author Share Posted March 22, 2011 I guess session is not what I need. My problem is not remind value of variable when refreshing the page. Let me explain my problem : Let's say I have 10 records on database. At first, display latest 3 records at beginning, so the value of $last_msg_id should be 8. Then user scroll down to the page, the page will display 2 older records which has id of 7 and 6, so the value of $last_msg_id should be 6. Then user scroll down again, value of $last_msg_id should be 4. After 10 seconds, the page reload, currently the value of $last_msg_id is still remain as 4. But when it auto call message1.php to get 3 of the latest records, the value of $last_msg_id become be 8. So when user scroll down again to get 2 older records, it will shows the same records as previously which has id of 7 and 6. It is wrong, the user already scrolled down for 3 times, so the page should show the records which have id of 3 and 2. 1st time scroll = 7,6 2nd time scroll = 5,4 3rd time scroll = 3,2 Quote Link to comment Share on other sites More sharing options...
zac1987 Posted March 22, 2011 Author Share Posted March 22, 2011 so my real problem is the conflict between the two features below : 1) feature of reload page for every 10 seconds to get new records. 2) feature of get older records when user scroll down to the page. Because both features above are depend on the same variable $last_msg_id, how can I solve the conflict between the two features? Quote Link to comment Share on other sites More sharing options...
aabid Posted March 23, 2011 Share Posted March 23, 2011 when message2.php is called set a session variable which stores the value of last_msg and set the condition in both the scripts like if(isset($_SESSION[lasg_msg]) { do things thet you want } else { continue with the same } Quote Link to comment Share on other sites More sharing options...
zac1987 Posted March 24, 2011 Author Share Posted March 24, 2011 Nice! Thank you. 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.