EllisLab text mark
Advanced Search
     
how to pass row( ) data from a model class function to a controller
Posted: 17 November 2012 06:12 AM   [ Ignore ]
Joined: 2012-11-16
3 posts

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:

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:

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

 
Posted: 17 November 2012 06:26 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Joined: 2012-11-16
3 posts

is it not something like this?

$fullname $this->loginscript->checklogin->fullname

thought that doesn’t work :(

Ta

 
Posted: 17 November 2012 06:38 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

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.

 

 

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 17 November 2012 06:42 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

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().

 Signature 

Stick with it, practice it and have fun with it.

 
Posted: 17 November 2012 06:54 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2012-11-16
3 posts

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

 
Posted: 17 November 2012 07:07 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2011-08-08
524 posts

Your welcome.

 Signature 

Stick with it, practice it and have fun with it.