CodeIgniter Forums
How can I get the current post's index by slug? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: How can I get the current post's index by slug? (/thread-78552.html)



How can I get the current post's index by slug? - Ajax30 - 02-06-2021

I am working on an online newspaper/blogging application with CodeIgniter 3.1.8 and Bootstrap 4.

At the bottom of the single post view, I want to add a link to the next post (as well as one to the previous post). For this, I need to get the data (slug, title, etc), of the next post (row in the posts table).

For this purpose, I have added this methods to my Posts_model model:


PHP Code:
/* Next post */
public function get_next_post($slug) {
        $query $this->db->get('posts');
        $row_index 6;
        $data $query->row_array($row_index);    
        
if ($query->num_rows() > 0) {
            $data $query->next_row();
            return $data;
        }
}

/* Prev post */
public function get_prev_post($slug) {
        $query $this->db->get('posts');
        $row_index 6;
        $data $query->row_array($row_index);    
        
if ($query->num_rows() > 0) {
            $data $query->previous_row();
            return $data;
        }
 


IMPORTANT:

The posts are displayed newest on top:

PHP Code:
public function get_posts($limit$offset) {
    $this->db->select('posts.*, categories.name as post_category, authors.first_name as author_fname, authors.last_name as author_lname');
    $this->db->order_by('posts.id''DESC');
        
$this->db->join('categories''posts.cat_id = categories.id''inner');
        
$this->db->join('authors''posts.author_id = authors.id''inner');
    $query $this->db->get('posts'$limit$offset);
    return $query->result();



If I could get the current post's index by slug, I could replace this hardcoded index of the 7th post - [font=Monaco, Consolas, Courier, monospace]$r[/font][font=Monaco, Consolas, Courier, monospace]ow_index = 6 [/font]- and the problem would be solved.

How do I do that?


RE: How can I get the current post's index by slug? - CINewb - 02-08-2021

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


RE: How can I get the current post's index by slug? - Ajax30 - 02-08-2021

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