0

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);
    }
MissCoder87
  • 2,669
  • 10
  • 47
  • 82
  • See if this post helps: http://stackoverflow.com/questions/6234159/codeigniter-cant-access-this-within-function-in-view It's for a view instead of a helper, but same problem. – John McMahon Apr 05 '15 at 18:38

1 Answers1

0

$this is a reference to the controller object instance. You can't refer to $this directly in your helper function. You can use the get_instance helper function to access an instance of the currently running controller instance.

To make a long story short, update your site_helper:

if(!function_exists('c_userexists'))
{
    function c_userexists($value)
    {
        $CI =& get_instance();
        $CI->db->select('count(*) as user_count');
        $CI->db->from('users');
        $CI->db->where('email', $userId);

        $query = $CI->db->get();
        if($query > 0){
            return true;
        } else {
            return false;
        }
    }
}

For more information, check out: http://www.codeigniter.com/userguide3/general/ancillary_classes.html?highlight=get_instance#get_instance

luciddreamz
  • 2,073
  • 14
  • 15