Hey, thanks for the help.
Getting the next (newer)
post works:
PHP Code:
public function get_next_post($slug) {
$this->db->where('id >', $this->get_post($slug)->id);
$this->db->limit(1);
return $this->db->get('posts')->row_array();
}
Getting the previous (older)
post does not work:
PHP Code:
public function get_prev_post($slug) {
$this->db->where('id <', $this->get_post($slug)->id);
$this->db->limit(1);
return $this->db->get('posts')->row_array();
}
It always returns the first post (lowest id) in the entire posts table.
The posts are ordered by
id, DESCENDING (heigh ids on top of low ones).
How do I fix this?
(02-08-2021, 09:40 AM)CINewb Wrote: What determines the order of the posts? Is it the ID, or the date? Why not create a function where you pass in the ID or the date of the current post, to get the next one. The SQL would be something like:
SELECT * FROM `posts` WHERE `ID` > ? ORDER BY `ID` ASC LIMIT 1
This will get you the next post in the list by ID. If your posts are ordered by date rather than ID, it would be:
SELECT * FROM `posts` WHERE `date` > ? ORDER BY `date` ASC LIMIT 1