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

时间:2013-04-11 18:50:16

标签: php codeigniter

我正在构建我的评论模块并试图让活动记录显示在视图上。看起来很简单但是我有很多事情要继续使用单独的模型和控制器,然后生成视图。

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

控制器:

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.

            }

        }

并尝试在此视图上显示它(由配置文件功能生成)

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

我似乎无法将它已经正确创建的数组变量传递给该视图?

提前感谢。

2 个答案:

答案 0 :(得分:1)

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

        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!

答案 1 :(得分:0)

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

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

否则: 您需要在loader类中创建一个新方法,以便在路由器中单独加载控制器。

如果您真的经过快速修复后直到找到更好的替代方案,您可以(取决于两个控制器是否位于同一目录中)只需在“配置文件”控制器中包含“注释”控制器并尝试这样的操作:

{缺点: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);
    }


}
相关问题