phpcodeignitermodel-view-controllerreturn

How to return two separate payloads from a single CodeIgniter model method


I am having trouble getting two tables and passing them to controller:

Model:

public function get_all_entries()
{
    $query = $this->db->get('entry');
    return $query->result();

    $this->db->select('entry_id , count(comment_id) as total_comment');    
    $this->db->group_by('entry_id');
    $comment = $this->db->get('comment');
    return $comment->result(); 
}

Controller:

$data['query'] = $this->blog_model->get_all_entries(); 
$this->load->view('blog/index', $data);

How do I return $query and $comment variables to controller? I think I am doing it wrong.


Solution

  • Use this because you are not allowed to return twice in the same method

    function get_all_entries() 
    {
        $query  = $this->db->get('entry');
        $data[] = $query->result();
    
        $this->db->select('entry_id , count(comment_id) as total_comment');    
        $this->db->group_by('entry_id');
        $comment = $this->db->get('comment');
        $data[] =   $comment->result(); 
        return $data;
    }
    

    EDITS:

    In controller

    function index(){
        $this->load->model('mymodel');
        $result = $this->mymodel->get_all_entries();
        $entries = $result[0] ;
        $comments = $result[1] ;
    
        $data['entries'] =  $entries;
        $data['comments '] =  $comments;
    
    }