我正在尝试从一个函数为属性赋值,并在另一个函数中访问它。我认为这应该就这么简单。不幸的是,我似乎无法处理这个简单的任务。
这是我的控制器:
<?php
class Form_Controller extends CI_Controller {
public $current_user;
public $pi = 3.14;
function index() {
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username');
$this->form_validation->set_rules('password', 'Password');
if ($this->form_validation->run() == FALSE) {
$this->load->view('login_form');
} else {
$this->load->model('Admin_Users_Model');
$is_logged_in = $this->Admin_Users_Model->login();
if ($is_logged_in) {
$this->load->view('admin_panel/panel');
$this->load->view('admin_panel/account_verification_status_form');
} else {
$this->load->view('login_form');
}
}
}
function account_verification() {
$this->load->model('User_Id_Model');
$data['user_data'] = $this->User_Id_Model->get_user_credentials();
$this->current_user = $data['user_data'];
foreach ($this->current_user as $value) {
echo $value;
}
$this->load->view('admin_panel/panel');
$this->load->view('admin_panel/account_verification_status_form');
$this->load->view('admin_panel/account_verification_name_form', $data);
$this->load->view('admin_panel/account_verification_status', $data);
}
function name_verification() {
$selected_value = $this->input->post('verify-name');
echo count($this->current_user) . '<br />';
echo $this->current_user;
echo $this->pi;
$this->account_verification();
}
private function _write_to_database($user_data) {
$this->load->model('User_Id_Model');
$this->User_Id_Model->write_to_database($user_data);
}
}
?>
当我致电account_verification()
时,$current_user
会被分配一个值,我可以打印它。之后我打电话给name_verification()
,但$current_user
没有价值。
如果您知道这里发生了什么,请解释。
答案 0 :(得分:1)
首先看看你的代码后
1)如果你从url调用name_varification而不是你没有为全局varibale分配任何值,即
$this->current_user.
2)如果你从url调用account_varification,而不是将你的assigne数据调用到golbal变量
$this->current_user
并且你没有在account_varification中调用name_varification,所以它再次意味着你是从url调用它的,因此它会丢失它的值,所以正确的代码要么调用first name_varificaton并将其分配给数据。
$this->load->model('User_Id_Model');
$data['user_data'] = $this->User_Id_Model->get_user_credentials();
$this->current_user = $data['user_data'];
之后调用$ this-&gt; account_verification就像你正在调用两个函数都会有值
我希望你理解。 问候 伊姆兰卡西姆