2

Hi i am a new programmer, i have a PHP & Mysql based pagination, i want to add Ajax functionality to it. i have gone through many tutorials but i was not able to find any which tells about adding ajax onto existing pagination they all tell about making Ajax based pagination.

i want user be able to paginate even if javascript is turned off. so i want to add some Ajax to my code so that i can be able to paginate with Ajax and PHP.

i can use jquery .load() method to paginate.

please look at my code and suggest me how i can fetch page url for ajax to paginate i guess something like this has to work. i cant figure out how, please help. or tell me some tutorial i can learn from.

Jquery Code

    $(document).ready(function(){

    $('#pagination').click(function(){
    $('pageurl').load('is-test2.php #PaginationDiv');});        
}); 

PHP & MySQL Based Pagination

<?php
    require_once('_ls-global/php/connection.php'); 
$db = mysql_select_db($database,$connection) or trigger_error("SQL", E_USER_ERROR);
$sql1 = "SELECT COUNT(*) FROM $table";
$result1 = mysql_query($sql1, $connection) or trigger_error("SQL", E_USER_ERROR);
$row = mysql_fetch_row($result1);
$numrows = $row[0];
$rowsperpage = 2;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['page']) && is_numeric($_GET['page'])) {
   $currentpage = (int) $_GET['page'];
} else {
   $currentpage = 1;
}
if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
   $currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql2 = "SELECT * FROM internet_security ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result2 = mysql_query($sql2, $connection) or trigger_error("SQL", E_USER_ERROR);
$list = mysql_fetch_assoc($result2);
$startrow = ($currentpage-1) * $rowsperpage;

Code in html

h3>Results <?php echo ($startrow+1) ?> - <?php echo min($startrow + $rowsperpage, $row) ?> of <?php echo ($totalpages *$rowsperpage) ?></h3>
<ul><?php 
if ($currentpage!=$totalpages) {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$totalpages'>$totalpages</a></li> ";
$nextpage = $currentpage + 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>Next&raquo;&raquo;</a></li> ";
}?></ul>



<ul><?php    
if($currentpage<$totalpages){
for ($x = ($currentpage - 3); $x < (($currentpage + 3) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo " <li id='pcurrent'><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li>";
} else {
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a></li> ";
}}}  
}
}
?> </ul>


<ul><?php
if ($currentpage > 1){
$prevpage = $currentpage - 1;
echo " <li><a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>&laquo;&laquo;Prev</a></li> ";
echo "<li><a href='{$_SERVER['PHP_SELF']}?page=1'>1</a></li> ";
}?></ul>

1 Answer 1

0

since you are getting your page variable into the PHP script with GET method, you can pass the variable like :

$(document).ready(function(){
    $('#pagination ul li a').click(function(){
        e.preventDefault();
        $('#divtoreplace').load($(this).attr("href"));            
    });        
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

$('#temp').load($(this).attr("href")+' #temp'); modified a bit and working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.