tags:

views:

115

answers:

2

Hello,

Currently i have a php file which fetch data from mysql to display in website.

I'm using input value to send as $_GET parameter to php file to determine the data to show.

mysql_query("SELECT * FROM messages WHERE msg_id>'$refID' ORDER BY msg_id DESC");
//$refID is input value

So once it load,

i'm using this jquery code to display it on website

setInterval(
function ()
{
$.get('load.php?id='+refID, function(html) { 
  $("ol#update").prepend(html);
  $("ol#update li:first").slideDown("slow");
});
}, 10000);

My question is how do i stop it from keep on repeating the same message? i want it to display if there is new data.

+1  A: 

Update:

How does your response look like? The easiest way would be to update refID and set it to largest msg_id in your response. This way, you will always only get new data.

I would edit the PHP code that it produces HTML code in this format:

<li id="msg-msg_id"><div>user avatar </div><div> user posted msg</div></li> 

where you replace msg_id with the actual ID if the message (e.g. 200), e.g.

<li id="msg-200"><div>avatar</div><div>my message</div></li> 

Then you can do the following with jQuery:

$.get('load.php?id='+refID, function(html) {
  if(html) { // html will be empty (I guess?) if there are no new messages
      refID = $('<div>' + html + '</div>').find('li:first').attr('id').split('-',2)[1];
      $("ol#update").prepend(html);
      $("ol#update li:first").slideDown("slow");
  }
});

This will update refID to the ID of the currently retrieved message and the next time the functions gets executed it will retrieve the messages from this ID on.


(Old answer didn't help)

Felix Kling
hi, its still keep on multiplying the same message. thank you
newinjs
@newinjs: Have you seen my change? I changed `prepend()` to `html()`
Felix Kling
problem with html() is that, it erased the old data on ol#update
newinjs
@newinjs: I thought that the response contains old data? Please see my update answer. If this does not help, please clarify your question because then I don't know what you actual problem is.
Felix Kling
message will get update if there is new data. example, it could be, id: 200, data : aaaaaaaa, id: 201, data : bbbbbbbbb. (WHERE msg_id>'$refID')if i used html(), the data aaaaaaa is gone.
newinjs
@newinjs: This still does not help me. Do you update `refID` somewhere? Or does it stay the same as long as you don't refresh the page? How does an actual response from your server look like?
Felix Kling
sorry, yes the input value will stay as let say id 200(last id of message at the time), so whenever a new data is there load.php will get the latest data >200 (WHERE msg_id>'$refID')
newinjs
@newinjs: Ok. So as I said in my updated answer, why don't know update `refID` to be the ID of the latest message? But as long as you don't show how your response look like, I cannot help you.
Felix Kling
response = data? english is not my native language. refID = $("#refID").val(); // let say 200, how do i update refID somehow?
newinjs
@newinjs: Ok sorry. I mean, `refID` is your "starting" number right? You want to get all messages that have a number higher then `refID`. So if get those messages, lets say messages with IDs 201, 202 and 203, the highest message number is now 203. Now if you set `refID` to 203 you will only get the new messages, higher than 203 and so on. With *response* I mean what is the data you get from your server, the data that is contained in your `html` parameter. It seems to be HTML but I would like to know how it looks like. Do you include the message IDs?
Felix Kling
oh what i thought initially about response, the data is user posted message on comment box. how do i set the refID? the refID get by php at load of page. i am new in this. thank you for helping me. cheers
newinjs
@newinjs: You are prepending data to your element: `$("ol#update").prepend(html);` What is the value of `html`? How does it look like? Maybe you can also post your PHP code? Just edit your question.
Felix Kling
< li>< div>user avatar </ div>< div> user posted msg</ div></ li>
newinjs
@newinjs: Please see my updated answer.
Felix Kling
your latest solution, in firefox, script stack space quota is exhausted. in chrome, it keep on multiplying again. thank you again for helping me
newinjs
It probably has to be `$('<div>' + html + '</div>').find('li...`
Felix Kling
+1  A: 

If i have understood right, you want:

  1. add the new message if is not dupe;
  2. mantain the other unique messages

so you need to loop i think like this:

$.get('load.php?id=' + refID, function(html) {
    var check = false;
    $("ol#update li").each(function() {
        if (this.id == refID) check = true;
    });
    if (check == false) 
    $("ol#update").prepend('<li id="' + refID + '">' + html + '</li>');
    $("#" + refID).slideDown("slow");
});

Assuming you have

<ol id="update">
<li id="200">The Brown Fox Jump Over the Lazy Dog</li>
....
</ol>

and Assuming your HTML response come not included into a <LI> tag!

aSeptik