[eluser]jordanarseno[/eluser]
Hey Guys,
I have a sign-up form at
localhost/CI/login/signup.
The form validation checks for valid username, password, email - all that junk properly.
I'm trying to take the username that is given, say johnsmith, verify it does not exist and add it to the database. If the username
does exist, I want the
validation_errors() box to display "username
johnsmith exists - try again"
The following is a function called
create_user() - part of my
Signup Controller which is launched when users click the Create User button in the view:
Code:
function create_user()
{
$this->form_validation->set_rules("firstname", "Name", "trim|required|max_length[20]");
$this->form_validation->set_rules("lastname", "Last Name", "trim|required|max_length[20]");
$this->form_validation->set_rules("email", "Email", "trim|required|valid_email|max_length[40]");
$this->form_validation->set_rules("username", "Username", "trim|required|min_length[4]|max_length[32]");
$this->form_validation->set_rules("password", "Password", "trim|required|min_length[4]|max_length[32]");
$this->form_validation->set_rules("password2", "Password Confirm", "trim|required|matches[password]");
//run form validation, if fail refresh page ans show errors
if($this->form_validation->run() == FALSE ) $this->index();
//otherwise...
else
{
//load the users model and check if the username exists
$this->load->model("users_model");
$row = $this->users_model->username_exists();
//if it does not exist
if(!$row)
{
//create the user
$this->users_model->create_user();
$data["main_content"] = "signup_successful_view";
$this->load->view("includes/logged_out_template", $data);
}
else
{
$this->form_validation->set_message('matches',"Username Exists");
$this->form_validation->matches($row->username, 'username');
$this->form_validation->run();
$this->index();
}
}
}
The
username_exists() function inside the
users_model is designed to return false if it DOES NOT exist, and to return the username itself (in the form of a row) if it DOES exist:
Code:
function username_exists()
{
$this->db->select("username");
$this->db->from("users");
$this->db->where("username", $this->input->post("username"));
$q = $this->db->get();
if($q->num_rows()==1) return $q->row();
else return FALSE;
}
The
username_exists() function is correct, what I'm really having trouble with is the last
else chunk in the large controller code above.
Any ideas? -
Thanks a million!