I’m trying to create a simple image gallery, showing 16 images per page. I’m using LIMIT 16 to display the correct amount on the page, but if there are more than 16 rows, I want to have links at the bottom, allowing the user to navigate to the next page.
I know I could achieve the desired result by removing the limit and simply using a loop to display the first 16 items, but this would be inefficient. Obviously, COUNTING the number of rows would always = 16.
$sql .= "posts p, images im, postimages pi WHERE
i.active = 1
AND pi.post_id = p.id
AND pi.image_id = im.image_id
ORDER BY created_at LIMIT 16";
Can anyone suggest a more efficient way of doing it?
Thanks
You need to offset your results for the given page you are on in addition to the separate query for getting the count that everyone else has mentioned.
The normal warnings about SQL injection would apply here.