Next/Previous Links within Secondary Category
#3395 (In Topic #637)
Navigate between news entries within the same secondary categoryGreetings,
Last week during our first Composr Community Chat, I asked Chris if there was a way to add next/previous buttons to the news entry screen, so users can navigate backward and forward through a category directly from the news pages, without having to go back to a category page between each one. Chris kindly wrote some miniblocks for this purpose, which he posted for download in this thread.
Unfortunately, I found that the blocks didn't work as expected on my own website, for several reasons. One of those reasons was that I utilize secondary categories for my video series, and the miniblocks Chris wrote were for primary categories. The other reason was something to do with timestamps, as I found that the "Next" link skipped over around 15 videos even within the primary category.
The reason secondary categories are tricky to work with is that they're stored in a separate table in the database. The first thing I did was modify Chris's miniblock to find the desired category from that second table, rather than using the intval($map['cat']) mechanism that Chris used, which only returns the primary category. Next, I added a second query which searches the second table for the ID of a news entry greater than the current ID that we're viewing. Using the ID instead of the timestamp means that I don't have to worry about cross-referencing the primary table that stores the timestamp, and it also means I don't have to worry about whatever sorting issue was happening before. It does assume that you're adding your news entries in the correct order and not rearranging the times afterwards, which is fine for my needs.
The "Next" button is now working flawlessly with secondary categories on my website. I haven't gotten the "Previous" button working quite yet. The query is returning the first news entry in the secondary category table located in the desired category, which means it's always returning Episode 1 of the series, no matter which episode is being viewed. We need the highest ID that's lower than the current ID. I'm not fluent enough in SQL/PHP/Composr to solve that one easily, so any suggestions are welcome, or I'll just come back to it and figure it out myself eventually.
Attached are the modified miniblocks as I'm using them now.
EDIT: I was correcting my code, but realised it would be going in another direction to yours, so I'm correcting yours.
$prev_id = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'news_entry', array('news_entry_category' => $secondary_cat), ' AND news_entry<' . strval($us_id));
$prev_id = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'MAX(news_entry)', array('news_entry_category' => $secondary_cat), ' AND news_entry<' . strval($us_id));
$next_id = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'news_entry', array('news_entry_category' => $secondary_cat), ' AND news_entry>' . strval($us_id));
$next_id = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'MIN(news_entry)', array('news_entry_category' => $secondary_cat), ' AND news_entry>' . strval($us_id));
i.e. MAX/MIN is needed to maximise/minimise the ID within the constraint given.
- If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
- If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
- If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
- If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
1 guest and 0 members have just viewed this.