Oct 21, 2014
0
Level 18
Previous/Next Record Pagination
I have bunch of entities, we'll call them articles, which are categorized through a pivot table.
Currently, I have a category page that shows all of the articles in that category along with pagination at the bottom. However, what I'm trying to accomplish now is; when you're viewing an individual article, I want to have links to the Previous/Next article in the same category.
How would I build a query to select the current article and also the Previous/Next article which belongs to the same category? Obviously, it'd be easy enough to create 3 separate queries, but I'd rather a more elegant solution.
Basically, I'm looking for a way to build a query like:
SELECT * FROM `articles` WHERE `id` = 10
UNION
#Select the first item less than 10
SELECT * FROM `articles` WHERE `id` = (
SELECT MAX(id) FROM `articles`
INNER JOIN `taggables` ON `articles`.`id` = `taggables`.`taggable_id`
WHERE
`taggables`.`tag_id` IN ('1')
AND `taggables`.`taggable_type` = 'Acme\\Models\\Article'
AND id < 10
)
UNION
#Select the first item greater than 10
SELECT * FROM `articles` WHERE `id` = (
SELECT MIN(id) FROM `articles`
INNER JOIN `taggables` ON `articles`.`id` = `taggables`.`taggable_id`
WHERE
`taggables`.`tag_id` IN ('1')
AND `taggables`.`taggable_type` = 'Acme\\Models\\Article'
AND id > 10
)
Thanks
Please or to participate in this conversation.