将参数传递给其他函数codeigniter

时间:2016-05-25 14:18:09

标签: php codeigniter

我想要将一个函数的传递变量传递给另一个函数我该怎么做。每个地方都用谷歌搜索但没有结果。

        public function blog() {

        $user_session_email = $this -> session -> userdata('user_email','name','id');
        if(!$user_session_email) {
            redirect('/');
        } 

        $user_data = $this -> users_model -> MyAccount([
            'id' => $this -> session -> userdata('id'),
            'name' => $this -> session -> userdata('name'),
            'email' => $this -> session -> userdata('user_email'),
        ]);
        $data["persoanl"] = $user_data;

        $returned_result = $this -> users_model -> load_messages();
        $messages["message"] = $returned_result['message'];

        $post_id = $this -> input -> post('post_id');
        $return_comments = $this -> users_model -> return_comments();
        $messages['comments'] = $return_comments;

        $this -> load -> view('users/inc/header', $data);
        $this -> load -> view('users/inc/blog_form', $data);
        $this -> load -> view('users/blog', $messages);
        $this -> load -> view('users/inc/footer');
    }

我想将 $ post_id 传递给以下函数

    public function load_messages() {
        $returned_result = $this -> users_model -> load_messages();
        $messages["message"] = $returned_result["message"];

        $return_comments = $this -> users_model -> return_comments();
        $messages['comments'] = $return_comments;

        $this -> load -> view('users/blog', $messages);
    }

2 个答案:

答案 0 :(得分:0)

您是否尝试将变量声明为全局?

        $post_id = null;
        public function blog() {
        global $post_id;
        $user_session_email = $this -> session -> userdata('user_email','name','id');

        if(!$user_session_email) {
            redirect('/');
        } 

        $user_data = $this -> users_model -> MyAccount([
            'id' => $this -> session -> userdata('id'),
            'name' => $this -> session -> userdata('name'),
            'email' => $this -> session -> userdata('user_email'),
        ]);
        $data["persoanl"] = $user_data;

        $returned_result = $this -> users_model -> load_messages();
        $messages["message"] = $returned_result['message'];

        $post_id = $this -> input -> post('post_id');
        $return_comments = $this -> users_model -> return_comments();
        $messages['comments'] = $return_comments;

        $this -> load -> view('users/inc/header', $data);
        $this -> load -> view('users/inc/blog_form', $data);
        $this -> load -> view('users/blog', $messages);
        $this -> load -> view('users/inc/footer');
    }

    public function load_messages() {
        global $post_id;
        $returned_result = $this -> users_model -> load_messages();
        $messages["message"] = $returned_result["message"];

        $return_comments = $this -> users_model -> return_comments();
        $messages['comments'] = $return_comments;

        $this -> load -> view('users/blog', $messages);
    }

答案 1 :(得分:0)

你不能做这样的事吗?

public function blog() {

        $user_session_email = $this -> session -> userdata('user_email','name','id');
        if(!$user_session_email) {
            redirect('/');
        } 

        $user_data = $this -> users_model -> MyAccount([
            'id' => $this -> session -> userdata('id'),
            'name' => $this -> session -> userdata('name'),
            'email' => $this -> session -> userdata('user_email'),
        ]);
        $data["persoanl"] = $user_data;

        $post_id = $this -> input -> post('post_id');
        $returned_result = $this -> users_model -> load_messages($post_id);
        $messages["message"] = $returned_result['message'];

        $return_comments = $this -> users_model -> return_comments();
        $messages['comments'] = $return_comments;

        $this -> load -> view('users/inc/header', $data);
        $this -> load -> view('users/inc/blog_form', $data);
        $this -> load -> view('users/blog', $messages);
        $this -> load -> view('users/inc/footer');
    }

在调用load_messages函数之前,请确保获取post_id。然后当你调用它时,你将$ post_id传递给...

public function load_messages($post_id) {
     // Use $post_id as you wish...
     $returned_result = $this -> users_model -> load_messages();
     $messages["message"] = $returned_result["message"];

     $return_comments = $this -> users_model -> return_comments();
     $messages['comments'] = $return_comments;

     $this -> load -> view('users/blog', $messages);
}