Big disclaimer to start with: I'm still super new to php & js, so please be gentle... haha
Anyway, I'm trying to create a simple infinite scroll that retrieves posts from a mysql table. I've cobbled together code from various internet tutorials to create something that is accessing and retrieving information ok. The problem is the number of posts and which one's it's retrieving.
With "$perpage" in the PHP file set to 10, The first 20 posts load, then every time it loads "new posts", it just loads posts 11-20 over and over. I imagine it's probably something simple, but my level of php & js is really only at the level where I can sort of understand it to read, but can't write it.
I've tried adding in LIMIT to the end of the $sql line, but that just seemed to break the code altogether.
Any help would be greatly appreciated!
If you want to see this in action in real life, the (very) draft site is here: site
$(document).ready(function(){
function getresult(url) {
$.ajax({
url: url,
type: "GET",
data: {rowcount:$("#rowcount").val()},
beforeSend: function(){
$('#loader-icon').show();
},
complete: function(){
$('#loader-icon').hide();
},
success: function(data){
$("#faq-result").append(data);
},
error: function(){}
});
}
$(window).scroll(function(){
if ($(window).scrollTop() == $(document).height() - $(window).height()){
if($(".pagenum:last").val() <= $(".total-page").val()) {
var pagenum = parseInt($(".pagenum:last").val()) + 1;
getresult('../php/best_result.php?page='+pagenum);
}
}
});
});
<?php
require_once("login.php");
$db_handle = new DBController();
$perPage = 10;
mysql_query("SET NAMES utf8");
$sql = '
SELECT letter, dayfield, monthfield, yearfield, nickname, country
FROM BestWorst
WHERE `lettertype` = "best" ORDER BY `ID` DESC';
$page = 1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;
$query = $sql . " limit " . $start . "," . $perPage;
$faq = $db_handle->runQuery($query);
if(empty($_GET["rowcount"])) {
$_GET["rowcount"] = $db_handle->numRows($sql);
}
$pages = ceil($_GET["rowcount"]/$perPage);
$output = '';
if(!empty($faq)) {
$output .= '<input type="hidden" class="pagenum" value="' . $page . '" /><input type="hidden" class="total-page" value="' . $pages . '" />';
foreach($faq as $k=>$v) {
$output .= '<div class="entry wow fadeInUp lefttext"><br/><p>'
. nl2br($faq[$k]["letter"])
. '</p><br/><p class="small" align="right">- This story took place on '
. $faq[$k]["dayfield"] . '/'
. $faq[$k]["monthfield"] . '/'
. $faq[$k]["yearfield"]
. '. Posted by ' . $faq[$k]["nickname"]
. ' from ' . $faq[$k]["country"]
.'<p></div><br/><br/><br/>';
}
}
print $output;
?>
$.ajax()and that the expected query is being built for MySQL?pagenumbut there's only one output by the PHP. And it just outputs the passed value from$_GET["page"]without changing it.:lastin his selector. Each time the jQuery completes it adds more html. That html will have anotherclass="pagenum"element. After he fjnds the value, he adds 1 to it and then uses that incremented value to build his url.$querythe actual query you expect it to be? So if you were toecho $query;, is what you see, what you expect AND does that query work as expected in phpMyAdmin (or whatever tool that you use to run SQL queries directly on the database)