Need help with jquery json data transfer from php file
Posted
by Scarface
on Stack Overflow
See other posts from Stack Overflow
or by Scarface
Published on 2010-04-16T18:15:37Z
Indexed on
2010/04/16
21:13 UTC
Read the original article
Hit count: 152
Hey guys I am trying to return the latest 10 results of a query from a php file, through json format, to a jquery getjson function that prints results. I am getting weird problems though. For example I am only getting 8 entries returned, and some are disordered, and sometimes nothing is returned. I am not really sure what I am doing wrong, so if anyone has any ideas I would really appreciate it.
This is my query ($res)
SELECT time, user, message FROM comments WHERE topic_id='$topic_id' ORDER BY time DESC LIMIT 10
This is the processing of the results
while($row = mysql_fetch_array($res)){
$message=$row['message'];
$user=$row['user'];
if($row['message'] AND $row['time'] > $_GET['time'])
$data[] = $row;
}
$out = json_encode($data);
print $out;
And this is the retrieval where prepare is just a function that returns information into a div
$.getJSON(files+"processing.php?action=load&time="+0+"&topic_id="+topic_id+"&t=" + (new Date()), function(json) {
if(json.length) {
for(i=0; i < 10; i++) {
$('#comment-list').prepend(prepare(json[i]));
$('#list-' + count).fadeIn(1500);
}
}
});
function prepare(response) {
count++;
var string = '<li class="comment-list" id="list-'+count+'">'
//organize info into a div
+'</li>';
return string;
}
© Stack Overflow or respective owner