此查询显示此活动记录


This query show me with this active record

我在获取两个表并将它们传递给控制器时遇到问题:

在模型中:

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(); 
}

在控制器中:

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

如何将$query$comment变量返回到控制器?我认为我做错了。

使用此选项是因为不允许在同一方法中返回两次

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

编辑:

控制器内

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

您的问题是首先返回$query->result()return函数会暂停当前函数,因此不会处理接下来的步骤。

最好的方法是为$queryget和$commentget创建两个方法。

你的问题的替代方案是

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

然后在你的控制器

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