如何在具有多个控制器的编码器中显示活动记录


How do I display active record in codeigniter with multiple controllers?

我正在构建我的评论模块,并试图让活动记录显示在视图上。似乎很简单,但我有很多正在进行和使用一个单独的模型和控制器,然后什么视图正在生成。

所以我有一个个人资料页面(我试图得到的结果显示)

控制器:

public function profile() 
      {
            $this->load->helper('url'); //Include this line
            $this->load->helper('date');
            $this->load->library('session');
            $this->load->library('form_validation');
            $session_id = $this->session->userdata('id'); //Ensure that this session is valid

            //TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
            $user_get = $this->uri->segment(3); // Modify this line

            // LOAD THE CORRECT USER
            $this->load->model('account_model');
            $user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
            $data['user'] = $user;
            $data['session_id'] = $session_id;


            if($user_get != $user['id'] || !$user_get)
            {
                $data['main_content'] = 'account/notfound';
                $this->load->view('includes/templates/profile_template', $data);
            }
            else
            {
            if($user_get == $session_id) //Modify this line also
            {
                $data['profile_icon'] = 'edit';
            }
            else
            {
                $data['profile_icon'] = 'profile';
            }
            $sharpie = $this->sharpie($user);
            $data['sharpie'] = $sharpie;
            $data['main_content'] = 'account/profile';
            $this->load->view('includes/templates/profile_template', $data);
            }
        }

现在我有一个新的控制器来显示我的注释:

public function airwave() {
        $this->load->helper('date');
        $this->load->library('session');
        $airwave_get = $this->uri->segment(3); 
        $this->load->model('community_model');
        $airwave = $this->coummunity_model->airwave($airwave_get); 
        $data['airwave'] = $airwave;
        $data['main_content'] = 'account/profile';
        $this->load->view('includes/templates/main_page_template', $data);
    }

使用这个模型:

public function airwave($id=null) 
        {
            if(is_null($id)) {
                $session = $this->session->userdata('is_logged_in');
                $commenter_id = $this->session->userdata('id');
            }else{
                $commenter_id = intval($id);
            }
            $query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
            if($query->num_rows()==1)
            {
               $data = $query->result_array();
               return $data[0];
            //above returns the single row you need to the controller as an array.
            //to the $data['user'] variable.
            }
        }

并尝试在这个视图(由profile函数生成)上显示

<div class="profile_airwave_comment_text">
    <?php echo $airwave['comment'];?>
</div>

我似乎不能让它传递数组变量我已经创建正确的视图?

我看到的第一个明显的错误是在你的模型的无线电波功能:

        if($query->num_rows()==1)
        {
           $data = $query->result_array();
           return $data[0];

你假设在你的查询中只有一个结果,所以如果有多于或少于一个,你的$data数组将不会被设置。

而且,即使这样,也只返回数组中的第一个元素。在你的问题中,我没有看到你想只返回一个。

最后,在您的视图中,如果您现在返回整个数组,而不仅仅是一个元素,则必须执行foreach语句。

编辑:请参阅建议的解决方案。

在您的模型中,不必担心检查结果数组中的数据量。稍后再讲。只是做:

return $query->result_array();

现在,保持你的控制器不变,但调整你的视图:

<div class="profile_airwave_comment_text">
<?php 
    if (isset($airwave) && is_array($airwave) && !empty($airwave))
    {
        foreach ($airwave as $wave)
        {
            echo $wave['comment'];
        }
    }
    else
    {
        echo 'No comments found...';
    }
?>
</div>

这应该可以工作,如果不行,至少让您知道没有注释被喜欢,从而检查您的数据库。

HTH !

如果您使用HMVC,只需从配置文件视图中调用该模块,即:

echo Modules::run('comments/profileComments', $user->id);

:你需要在加载器类中创建一个新方法,以便与路由分开加载控制器。

如果你真的需要一个快速修复,直到你想出一个更好的替代方案,你可以(取决于两个控制器是否在同一个目录中)只是将'comments'控制器包含在你的'profile'控制器中,并尝试这样做:

{缺点:CI分页不能工作!}

应用程序/控制器/profile.php

include 'Comments' . EXT;
class Profile extends CI_Controller{
    public function __construct ()
    {
        parent::__construct () ;
    }
    public function index( $user_id ){
        $commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
        return $this->load->view($this->template, array(
               'view' => 'profiles/_index',
               'comments' => $commentsPartialView
        ));
    }

}

应用程序/控制器/Comments.php

class Comments extends CI_Controller{
    public function __construct ()
    {
        parent::__construct () ;
    }
    public function getProfileComments( $user_id ){
        $user_comments = ''; //pull from DB
        //set third param to TRUE for partial view, without main template
        return $this->load->view('comments/show', array(
               'user_comments' => $user_comments
        ), TRUE);
    }

}