ajax pagination - Can't get last table row


ajax pagination - Can't get last table row



This code works perfectlly. But I can't get last row of the table.
ex: My table had 9 rows but this displying only 8 rows, 1 row missing. Missing row is the last row when I use ASC or DESC.
here's my code.. sorry my English not good.
thank you!



index.php


<?php
$db_username = 'root';
$db_password = '';
$db_name = 'ad_man';
$db_host = 'localhost';
$item_per_page = 2;
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
if($check_ad = mysqli_query($connecDB,"SELECT ad_uid FROM fullbanner WHERE ad_uid='501'")){
$countr=mysqli_num_rows($check_ad);
if($countr>=1){

$pages = $countr/$item_per_page;


//create pagination
if($pages > 1)
{
$pagination = '';
$pagination .= '<ul class="paginate">';
for($i = 1; $i<$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '</ul>';
}

}else {$pagination="<ul class='paginate'>Empty ADs</ul>";}}else {$pagination="Empty ADs";}//mama
?><!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax Pagination</title>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#results").load("fetch_pages.php", {'page':0}, function() {$("#1-page").addClass('active');}); //initial page number to load

$(".paginate_click").click(function (e) {

$("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>');

var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need

$('.paginate_click').removeClass('active'); //remove any active class

//post page number and load returned data into result element
//notice (page_num-1), subtract 1 to get actual starting point
$("#results").load("fetch_pages.php", {'page':(page_num-0)}, function(){

});

$(this).addClass('active'); //add active class to currently clicked element (style purpose)

return false; //prevent going to herf link
});
});
</script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="results"></div>
<?php echo $pagination; ?>
</body>
</html>



fetch_pages.php


<?php

include("config.inc.php"); //include config file

//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

//validate page number is really numaric
if(!is_numeric($page_number)){die('Invalid page number!');}

//get current starting point of records
$position = ($page_number * $item_per_page);

//Limit our results within a specified range.
$results = mysqli_query($connecDB,"SELECT * FROM fullbanner WHERE ad_uid='501' ORDER BY ad_count ASC LIMIT $position, $item_per_page");

//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["ad_count"].'">'.$row["ad_code"].'. <span class="page_name">'.$row["ad_title"].'</span><span class="page_message"><a href="../new/image_ads/'.$row["image_url"].'" target="_new"><< Viwe AD image >></a></span></li>';
}
echo '</ul>';
?>



thank you!





What is the value of $position used with LIMIT in fetch_pages.php ?
– Deepak
Nov 20 '14 at 12:30





$position = ($_POST["page"] * $item_per_page); Last page number @Deepak
– Varuna
Nov 20 '14 at 12:37


$position = ($_POST["page"] * $item_per_page);





Just do echo $position and see the value.
– Deepak
Nov 20 '14 at 12:40





1st page = 0 | 2nd = 4 | 3rd = 6 | 4th - 8 ... start with 0 but when I click 1st page it echo 2 @Deepak
– Varuna
Nov 20 '14 at 12:47




1 Answer
1



Check if the page number is equal to 0, if not simply divide $position by 2


//get current starting point of records
if ($position === 0) $position = $position;
else $position = ($position/2)



This worked for me






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter