Saturday, 15 June 2013

php - Passing data to model CI -



php - Passing data to model CI -

thanks @mischa helping me out here.

answer :

model :

function validate_login($username, $password) { $bcrypt = new bcrypt(17); $sql = "select * users username = ? "; $loginq = $this -> db -> query ($sql, array($username)); $database = $loginq->row(); $hash = $database->password; if ($bcrypt -> verify($password, $hash)){ homecoming $loginq; } }

controller :

function validate_credentials() { $this -> load -> library('form_validation'); $this -> load -> library('bcrypt'); $this -> form_validation -> set_rules('username', 'username', 'required|alpha_numeric|min_length[4]|max_length[15]'); $this -> form_validation -> set_rules('password', 'password', 'required|min_length[7]|alpha_dash|max_length[20]'); if ($this -> form_validation -> run() == false) { $this -> index(); } else { $this -> load -> library('bcrypt'); $this -> load -> model('login_model'); $username = $this -> input -> post('username'); $password= $this -> input -> post('password'); if ($loginq = $this -> login_model -> validate_login($username, $password)) { if ($activated = $this -> login_model -> activated($username)) { $session_array = array('username' => $this -> input -> post('username'), 'loggedin' => true); $this -> session -> set_userdata($session_array); redirect('staff_controller/index'); } else { $this -> session -> sess_destroy(); $this -> load -> view('accessdenied_view'); $this -> output -> _display(); die(); } } else { $this -> index(); } } }

what you're trying works when passing info views. have pass separate variables model. this:

function validate_login($username, $password) { $bcrypt = new bcrypt(17); $sql = "select * users username = ? limit ? "; $loginq = $this -> db -> query ($sql, array($username, 1)); $row = $loginq->result(); $hash = $row['password']; if ($brcrypt -> verify($password, $hash)){ homecoming $loginq; } }

of course of study means have alter controller code pass variables separately.

another alternative utilize $date['username'] , $data['password'] in model, wouldn't recommend that, because makes code harder read.

update create more clear:

controller:

$username = $this->input->post('username'); $password = $this->input->post('password'); $this->login_model->validate_login($username, $password);

model:

function validate_login($username, $password) { // etc. }

php mysql codeigniter select

No comments:

Post a Comment