I am trying to make a simple “load more” button where I have an ajax request a load more page which loads a few more database entries. My problem is I can do my select statement like so:
$result = $db->query("SELECT uid,username,image FROM tbl_multimedia LIMIT 3");
But every time I click it, I see the first 3 results, not the fourth and so forth. This code does not count or check to see which id has been loaded last and start from the next. I am trying to keep it simple, any quick ideas on what I can add here to make it count. I have the javascript part ready and working with AJAX, I am just stuck here.
My code is below:
try
{
// Query multimedia table
$result = $db->query("SELECT uid,username,image FROM tbl_multimedia LIMIT 3");
// Convert the object to a string
$result->unescape(array(
'username' => 'string',
'image' => 'string',
'uid' => 'integer'
));
// No users in our app :(
$result->tossIfNoRows();
?>
<?
// Magic happens -> Create rows with every name found in the database
foreach ($result as $row)
{
?>
<div class="g_holder">
<? echo '<a href="profile.php?user_id='.$row['uid'].'" ><img src="img/'.$row['image'].'" width="120" height="120"/></a>'; ?>
<div class="g_caption">
<p class="g_txt">
<? echo $row['username']; ?>
</p>
</div>
</div>
<?
}
}
catch (fNoRowsException $e)
{
?>
<p>noone cares about using my app!</p>
<?php
}
What you need is an offset. So your query should be like so:
Where offset is a number that increase by 3 after each click.
So the first time your page load it will be
0, after the first click$offset = 3, and the next$offset = 6, and so on.