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

(This post was last modified: 02-06-2021, 02:41 PM by Ajax30.)

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?
Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB