Welcome Guest, Not a member yet? Register   Sign In
How can I get the current post's index by slug?
#3

(This post was last modified: 02-08-2021, 04:30 PM by Ajax30.)

Hey, thanks for the help.

Getting the next (newer) [font=Tahoma, Verdana, Arial, sans-serif]post [/font]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) [font=Tahoma, Verdana, Arial, sans-serif]post does not [/font]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
Reply


Messages In This Thread
RE: How can I get the current post's index by slug? - by Ajax30 - 02-08-2021, 04:28 PM



Theme © iAndrew 2016 - Forum software by © MyBB