var url="php_paging-ajaxck.php";
var myendrecord=myForm.end_record.value;
url=url+"?endrecord="+myendrecord;
url=url+"&direction="+val;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
php_paging-ajaxck.php$endrecord=$_GET['endrecord'];//
if(strlen($endrecord) > 0 AND (!is_numeric($endrecord))){
echo "Data Error";
exit;
}
For paging we will find out total number of records present in our table matching to our requirement.
$total_records = $dbo->query("select count(id) from student")->fetchColumn();
We can set number of records to be displayed in a page.
$limit=10; // Number of records per page, you can change this value
Now based on direction button pressed we will set the value of start record. We will be using limit query to get a set of records from the table so start record is required for the query we will use to get the set of records from student table.
if(($endrecord) < $total_records ){$end="yes";} // managing forward button
else{$end="no";}
if(($endrecord) > $limit ){$startrecord="yes";} // managing reverse button
else{$startrecord="no";}
$main = array('data'=>$result,'value'=>array("endrecord"=>"$endrecord",
"limit"=>"$limit","end"=>"$end","startrecord"=>"$startrecord"));
echo json_encode($main);
var myObject = JSON.parse(httpxml.responseText);
Here myObject is an array with records and data. We know that it has one more array inside named data. We will loop through this array to display the records.
for(i=0;i<myObject.data.length;i++)
{
str = str + "<tr><td>" + myObject.data[i].id + " </td><td>" + myObject.data[i].name +
" </td><td>" + myObject.data[i].myclass + " </td><td>" + myObject.data[i].mark + "</td></tr>"
}
Now let us store the value of endrecord which is required for further posting back to backend script.
var endrecord=myObject.value.endrecord
We have two navigational buttons, Previous button and next button to move to previous and next set of records. At first page we need to hide Previous button and in last page we need to hide Next button. We will manage the display of buttons by using its style property.
myForm.end_record.value=endrecord;
if(myObject.value.end =="yes"){ document.getElementById("fwd").style.display='inline';
}else{document.getElementById("fwd").style.display='none';}
if(myObject.value.startrecord =="yes"){ document.getElementById("back").style.display='inline';
}else{document.getElementById("back").style.display='none';}
arifxxx | 13-06-2014 |
this is one of the best tutorial what i've know |
subbu | 26-02-2017 |
how to implement or add one more button that retrieve all records at once? can you provide the code for that? thanks. |
smo1234 | 28-02-2017 |
Your query reads like this $query=" SELECT * FROM student limit $eu, $limit "; If you remove limit part and make it like this. $query=" SELECT * FROM student "; You can display all the records. With the button you can pass a variable like this <a href=page_name.php?display_status=dispaly_all>Show all</a> Collect display_status like this at the top of the page. $display_all=$_GET['display_all']; if($dispaly_all=='display_all'){ $q='SELECT * FROM student'; }else{ $q='SELECT * FROM student limit $eu,$limit'; } |
Php Developer | 01-06-2018 |
Nice work! This is massively helpful to me. It just made my work easier. It saved lot of time. Thanks. |