I'm very new to codeigniter and i'm building some sample tools to get to know my way around it. I've followed some basic tutorials online now i'm going my own way.
I've got the following code, i'm trying to work out if a user exists before registering them. I also can't work out how to tell my view that the user already exists if it's an error, where do i pass that data back?
The error i'm getting is:
Fatal error: Using $this when not in object context in /Users/Tom/www/crm/application/helpers/site_helper.php on line 18
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);
}