0

hi, I am generating records through php/sql, as u can see the code displays 5 records, what Im trying to do is program so that when the next button is pressed the next 5 records will show?? any help much appreciated thank you,

the code -

function show(qstring,qtype){
 $("#vsdiv").show();
 var myurl="scripts/searchpage.php"; 
 $.ajax({
  url: myurl, dataType:'json',data:{querystring:qstring,querytype:qtype},
  success: function(member){
   $("#results").append('<tr class="me">'+
                                            '<th>#REC</th><th>name</th><th>surname</th>');
   var j=0;var totrecs=1;
   for(var i=0;i<5;i++){
    $("#results").append('<tr class="rows'+j+'">'+
                      '<td class="ids" id="z'+i+'">'+totrecs+'</td>'+
              '<td>'+name[i].bdate+'</td>'+
              '<td class="users" id="username'+i+'">'+member[i].username+'</td>'+
              '<td id="contact'+i+'">'+member[i].fname+' '+member[i].lname+'</td>'+
              '<td><input type="checkbox" name="whome" id="showMe'+i+'"'+
                                             'class="boxes" onclick="getMe('+i+')" /></td></tr>');    
      totrecs++;
      j++;
     }
    }
 });
}

and for the html button -

<input type="button" id="next5" title="Next 5 Records" disabled="disabled" value="Next 5>>" />
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by dcdruck
0

You're setting the limit to 5 by using a for loop to only look at the first 5 results returned from the HTTP request. That is basically returning all of the data and then taking only what you need. This is not efficient.

What you should be doing instead is using a LIMIT clause on your query and providing that data in your querystring to the url that is returning the data. If you're using MySQL, then LIMIT 0,5 would give limit the records returned to only the first 5. LIMIT 5,5 would return the next 5. LIMIT 10,5 the next. And so on. See the documentation here: http://dev.mysql.com/doc/refman/5.0/en/select.html.

So in your Javascript you could add to the query string something like page=2&recs_per_page=5. Then in your PHP that returns the data:

<?php
$page_number = $_GET['page'];
$recs_per_page = $_GET['recs_per_page'];
$page_number--;
$limit_start = ($page_number * $recs_per_page);
$limit_count = $recs_per_page;

// Your LIMIT clause on your query will now be:
//   LIMIT $limit_start,$limit_count
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.