4

I am trying to pass an array through a function in a controller class and retrieve it from another function in a class inside model, but the values cannot be retrieved. please advise.

The code is as follows.

Controller class code

 class home extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->helper('url');
}

function index() {
    $this->load->view('loginview');
}

function login() {
    $parameters = array(
        '$uname' => $this->input->post('uname'),
        '$passwords' => $this->input->post('password')
    );

    $this->load->model('loginModel');
    $validate = $this->loginModel->validateuser($parameters);

    if(count($validate)== 1){
        echo "Logged in";
    }
    else
    {
        //redirect('home/index');
        echo "dasad";
    }


}

}

Model class code

class loginModel extends CI_Model {

function __construct() {
    parent::__construct();
}

public function validateuser($parameters) {

        $uname = $parameters['uname'];
        $pass = sha1($mem['pass']);



    $query = $this->db->query("select * from user where username = '$uname' and password = '$pass'");
    $result = $query->result_array();
    return $result;

}

}

The variables $uname and $pass are the two values that need to get to query the database. please help

1

4 Answers 4

1

There are errors in your code. Correct ones of controller login() method:

function login() {

    // you don't need '$' sign
    $parameters = array(
        'name' => $this->input->post('uname'),
        'pass' => $this->input->post('password')
    );

    $this->load->model('loginModel');
    $validate = $this->loginModel->validateuser($parameters);

    // stuff..

}

and model loginModel() method:

public function validateuser($parameters) {

    $uname = $parameters['name'];
    $pass = sha1($parameters['pass']); // and second error was here

    // stuff..

}
Sign up to request clarification or add additional context in comments.

1 Comment

owh.... ya thanks a lot i have used $ in array keys in the controller that s the biggest problem thanks again...
1

Your array key strings are different in model and controller classes. Here are the possible fixes.

In Controller use this:

$parameters = array(
        'uname' => $this->input->post('uname'),
        'passwords' => $this->input->post('password')
    );

Secondly in your Model. Use this to get parameters.

  $uname = $parameters['uname'];
  $pass = sha1($parameters['passwords']);

Hope it helps.

2 Comments

This doesn't matter with the way the variables are being passed.
yes, using $ in array keys was the biggest problem and ya it was nt matching as well thanks a lot for the help....
0

I see your answer is already fixed but i just want to give you an alternative of doing it. It is more secure and short:

Controller:

function login() {

    $this->load->model('loginModel');
    $validate = $this->loginModel->validateuser();

    if($validate ){
        echo "Valid User";
    } else{
        echo "Invalid User";
    }
}

Model:

public function validateuser() {

 $query = $this->db->query("select * from user where username = ? and password = ?",array($this->input->post('uname'),sha1($this->input->post('password'))));
   if($query->num_rows() == 1){
      return true;
   }else{
     return false;
  }

} 

1 Comment

yes, it was solved. looks like your methods is better from what i have done... thanks alot for the suggestion.
0

I had the same issue and the only way to do this I found obviously was to json_encode() the array in the controller and json_decode them in the model. IT worked like a charm form me. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.