Welcome Guest, Not a member yet? Register   Sign In
how to pass row( ) data from a model class function to a controller

[eluser]Rob J Shillito[/eluser]

I have literally started using codeigniter and am still getting my head around how it works. I am creating a simple login for a web app, but not sure how, after I have created a query in a model class, I can get the data back to the controller so I can apply some logic to it.

here's my code in my Controller index function:

function index()
  $data['title'] = "Login Page";
  $data['login'] = "Please Login";
  $this->load->helper('form');//loads the form helper
  if( $this->input->post("submit") ) {
    $this->load->model('loginscript', '',TRUE);//true loads the db
    $this->loginscript->checklogin($_POST['username'], $_POST['password']);
     $fullname = $this->loginscript->checklogin->fullname;//doesn't work
      echo "win";
     else echo "fail";
     //****if the output is fine then redirect
     //redirect('/dashboard', 'location', 301);
  }//end of isset
   $this->load->view('loginpage', $data);//the pages in the view directory

The code at the moment, for testing purposes, is trying to get the fullname value, which is stored in the db.

Here's the model code:

function checklogin($username, $password){
  $data['query'] = $this->db->get('profiles');
  //hashes and salts the password to check against the db entry
  $salt = "test";
  $password = md5($salt.$password);
  $this -> db -> select('username, password, fullname');
  $this -> db -> from('profiles');
  $this -> db -> where('username', $username);//checks username
  $this -> db -> where('password', $password);//checks password
  $this -> db -> limit(1);
  $login = $this->db->get();

  if($login->num_rows() == 1)
    return $login->row();
   echo $rows->fullname;//works
   return false;
}//end of checklogin function

Could someone enlighten me how you can output each field from the database separately in the Controller class?

Very much appreciated Smile

Many thanks

Rob Shillito

[eluser]Rob J Shillito[/eluser]
is it not something like this?

$fullname = $this->loginscript->checklogin->fullname;

thought that doesn't work Sad


Not tested but try,

In your controller:
$fullname = $this->loginscript->checklogin('joe', 'pass_me');

Since it is stored as array, you can output for testing like this,
echo print_r($fullname);

You echo an array inside a controller for testing purpose only.


Once you saw the structure of the array.
You will echo or access it's key according to it's structure.

If this is not clear to you, please post the structure here of the array,
once your done outputting it using print_r().

[eluser]Rob J Shillito[/eluser]
Thanks very much mate Smile

All working now Smile

Just in case anyone wants the code here is the fix to output the fullname, which is stored in the db.

$output = $this->loginscript->checklogin($_POST['username'], $_POST['password']);//pass variables to function
echo $output->fullname;

Thanks again Smile

Your welcome.

Theme © iAndrew 2016 - Forum software by © MyBB