CodeIgniter Forums
how to pass row( ) data from a model class function to a controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: how to pass row( ) data from a model class function to a controller (/showthread.php?tid=55927)



how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

[eluser]Rob J Shillito[/eluser]
Hi,

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:

Code:
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
    
     if($fullname){
      echo "win";
     }
     else echo "fail";
    
     //****if the output is fine then redirect
     $this->load->helper('url');
     //redirect('/dashboard', 'location', 301);
  }//end of isset
  
  else
  {
   $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:

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
  }
  else
  {
   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


how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

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

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

thought that doesn't work Sad

Ta


how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

[eluser]solid9[/eluser]
Not tested but try,

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

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

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






how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

[eluser]solid9[/eluser]
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().


how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

[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.

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

Thanks again Smile


how to pass row( ) data from a model class function to a controller - El Forum - 11-17-2012

[eluser]solid9[/eluser]
Your welcome.