【发布时间】:2015-04-05 18:28:52
【问题描述】:
我对 codeigniter 很陌生,我正在构建一些示例工具来了解我的方法。我已经在网上学习了一些基本教程,现在我要走自己的路了。
我有以下代码,我正在尝试在注册之前确定用户是否存在。我也不知道如何告诉我的视图用户已经存在,如果它是一个错误,我在哪里将数据传回?
我得到的错误是:
致命错误:在第 18 行的 /Users/Tom/www/crm/application/helpers/site_helper.php 中不在对象上下文中使用 $this
controllers/users.php
public function register()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a new user';
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('surname', 'Surname', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('users/register');
$this->load->view('templates/footer');
}
else
{
if(c_userexists($this->input->post('email'))){
$this->load->view('templates/header', $data);
$this->load->view('users/register');
$this->load->view('templates/footer');
} else {
$this->users_model->set_register();
$this->load->view('users/success');
}
}
}
helpers/site_helper.php
if(!function_exists('c_userexists'))
{
function c_userexists($value)
{
$this->db->select('count(*) as user_count');
$this->db->from('users');
$this->db->where('email', $userId);
$query = $this->db->get();
if($query > 0){
return true;
} else {
return false;
}
}
}
models/Users_model.php
public function set_register()
{
$this->load->helper('url');
$data = array(
'firstname' => $this->input->post('firstname'),
'surname' => $this->input->post('surname'),
'email' => $this->input->post('email'),
'password' => c_passencode($this->input->post('email'))
);
return $this->db->insert('users', $data);
}
【问题讨论】:
-
看看这篇文章是否有帮助:stackoverflow.com/questions/6234159/… 这是一个视图而不是一个助手,但同样的问题。
标签: php codeigniter