Im trying to pull results from my database using AJAX when you scroll to the bottom of my page, my first 4 results are being duplicated however and placed at the bottom of my page, and then another 4 results dont actually appear for some unknown reason :/ Can anybody see anything wrong with my query?
// If county isnt set, show all data
$sth = $conn->prepare("SELECT * FROM directory WHERE user_active != ''ORDER BY Date DESC LIMIT 12");
$sth->execute(array(':county' => $county));
$c = 1;
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
echo '<div class="entry';
if (($c % 4) == 1) echo ' alpha ';
echo ' span3"><span class="name">' . $row['First_Name'] . ' ' . $row['Surname'] . "</span>";
echo MORE STUFF HERE IS OUTPUT
$c++;
}
Ive noticed if i remove the query limit, all results show correctly?
It sounds like you are trying to do AJAX-based pagination. I would imagine that the problem here is that you are not specifying an offset in your LIMIT
clause. The way you are doing it now, you will always get the same 12 results from your query (assuming of course that the table data didn't change). What you are likely needing to do is to specify your LIMIT
like this:
LIMIT <offset>, <number of records>
So if you want to get the first 12 records it would be:
LIMIT 0, 12
the next 12 would be:
LIMIT 12, 12
and so on...