1

I have checked codeigniter's documentations but there is no special page or information about it. All I wanna know is how it's working. I have this code and it's working!, What I'm wondering how is_unique working when there's no access to any model or database. Is there any class in codeigniter that is_unique uses it to retrieve data based on the information like the name of the table and the field you give it to? and that's the code:

Email input in the view:

echo form_input('email_address', set_value('email_address'), 'placeholder="Email"');

And the controller:

function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email|is_unique[membership.email]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');

        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('pages/signup_form');
        }

        else
        {           
            $this->load->model('admin/Users_model');

            if($query = $this->Users_model->create_member())
            {
                $this->load->view('pages/signup_successful');           
            }
            else
            {
                $this->load->view('pages/signup_form');         
            }
        }

    }

and her's the model:

function create_member()
    {

            $new_member_insert_data = array(
                'email' => $this->input->post('email_address'),         
                'user_name' => $this->input->post('username'),
                'pass_word' => md5($this->input->post('password'))                      
            );
            $insert = $this->db->insert('membership', $new_member_insert_data);
            return $insert;

    }

1 Answer 1

3

You can find the source code for this function at: system/libraries/Form_validation.php which appears to be the only instance is_unique is called from the system classes.

Code:

    /**
     * Is Unique
     *
     * Check if the input value doesn't already exist
     * in the specified database field.
     *
     * @param   string  $str
     * @param   string  $field
     * @return  bool
     */
    public function is_unique($str, $field)
    {
        sscanf($field, '%[^.].%[^.]', $table, $field);
        return isset($this->CI->db)
            ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
            : FALSE;
    }

"The function sscanf() is the input analog of printf(). sscanf() reads from the string str and interprets it according to the specified format, which is described in the documentation for sprintf()." - PHP Docs

sscanf() is running to separate the database from the table name.

Then the function checks if a database connection exists, selects the table supplied in the argument, and searches the table to return whether or not that entry exists. Using Ternary Operators (?:) with the syntax being boolean expression ? if true : if false;

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.