简体   繁体   中英

What causes this failure to get the data from the next row in a MySQL table, with Codeigniter 3?

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 method to my Posts_model model:

/* Next post */
public function get_next_post($slug) {
    $query = $this->db->get_where('posts', array('slug' => $slug));
    if ($query->num_rows() > 0) {
        $data = $query->next_row();
        return $data;
    }
}

In the controller I have:

public function post($slug) {
  //more code
  $data['post'] = $this->Posts_model->get_post($slug);
  $data['next_post'] = $this->Posts_model->get_next_post($slug);
  print_r($data['next_post']);
  //more code
}

EDIT: In the Posts_model , I now have:

/* 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;
    }
}

That means that if I could get the current post's index by slug, I could replace this hardcoded index of the 7th post - $row_index = 6 - and the problem would be solved.

How do I do that?

// Your_model.php    

...

public function getPost($slug) {
    $this->db->where('slug', $slug);
    return $this->db->get('posts_table')->row_array();
}

public function getPrevPost($currentPostId) {
    $this->db->where('id <', $currentPostId);
    $this->db->order_by('id', 'desc');        
    $this->db->limit(1);
    return $this->db->get('posts_table')->row_array();
}

public function getNextPost($currentPostId) {
    $this->db->where('id >', $currentPostId);
    $this->db->limit(1);
    return $this->db->get('posts_table')->row_array();
}


// Yourcontroller.php

...

public function getPost($slug) {
    $post = $this->your_model->getPost($slug);
    
    $data = [
            'thePost' => $post,
             ...
            'prevPost' => $this->your_model->getPrevPost($post['id']),
            'nextPost' => $this->your_model->getNextPost($post['id']),
             ...
            ];
    ...
}

EDIT: this post answers the original question. In the meantime below code was used in an edit by OP.

you need to return a result of your query: $data = $query->row_array();

And get_where() is limiting the record-set to one record, hence there is no next record. You need to return the complete record-set with $this->db->get('posts') . In case you know the row_number (eg: 5) of the row containing $slug , you can point to it. The next_row shown, is row number 6.

public function get_next_post($slug) {
    $query = $this->db->get('posts');  // querying the whole data-set
    $data = $query->row_array(5);      // the missing line
    if ($query->num_rows() > 0) {
        $data = $query->next_row();
        return $data;
    }
}

now you should get your next row (if exists), see Result Rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM