Why isn't this json/jquery function returning 10 results and why does it return 0 sometimes?

Posted by Scarface on Stack Overflow See other posts from Stack Overflow or by Scarface
Published on 2010-04-17T22:50:25Z Indexed on 2010/04/17 22:53 UTC
Read the original article Hit count: 155

Filed under:

Hey guys, quick question. I have a json function returning results from a php query but it is not working out because it seems to keep executing the query until all results are returned and sometimes returns 0 results (if I keep refreshing page, it returns 0 results sometimes). I want to limit results returned to 10 so I was wondering if anyone could point out the error of my code. Would really appreciate it. PS I am positive the query is returning the correct results and only 10 on its own. It just seems to be this one function that is not working out.

SELECT time, user, comment FROM comments WHERE topic_id='$topic_id' ORDER BY time DESC LIMIT 10

var count=0;

       function prepare(response) {
          count++;    
          var string = '<li class="list" id="list-'+count+'">'
              +'</li>';       
          return string;
        }

  $.getJSON(files+"comments.php?action=view&load=initial&topic_id="+topic_id+"&t=" + (new Date()), function(json) {
            if(json.length) {
              for(i=0; i < json.length; i++) {
                $('#shoutbox-list').prepend(prepare(json[i]));
                $('#list-' + count).fadeIn(1500);
              }
              var j = i-1;
              lastTime = json[j].time;
            }

          });

© Stack Overflow or respective owner

Related posts about jQuery