Jump to content

How to create twitter chat feature?


zac1987

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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? :confused:

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.