Welcome Guest, Not a member yet? Register   Sign In
Passing a variable from model to controller
#1

[eluser]Dandy_andy[/eluser]
I'm quite new to Codeigniter and PHP in general but I've managed to code my way through a lot of functionality for my website and I'm learning every day. However, I'm struggling with something that is probably very very basic. I want to pass the results of a variable that is set as a text string from a model to my controller. The variable is simply an error message that I want to appear in a particular view (but which I don't want to load up separately - I just want to pass this through the controller)

The model code is:-

Code:
function checkmatch()
  
{
  //select from database field names
     $this -> db -> select('mem_id, username, password, credits, mem_type');
  //from database table
     $this -> db -> from('members');
  //comparison (in this case where username and password matches the POSTED username and password value from the form
     $this -> db -> where('username = ' . "'" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'");
     //limit number of return rows to 1
  $this -> db -> limit(1);
    
  $usernamequery = $this -> db -> get();
   //logic control
    if($usernamequery -> num_rows() == 1)
   {
    //extract information from the relevant row in the database and convert to variable
    $member = $usernamequery->row_array();
    
    //set session data
    $this->session->set_userdata('mem_id', $member['mem_id']);
    $this->session->set_userdata('username', $member['username']);
    $this->session->set_userdata('credits', $member['credits']);
    $this->session->set_userdata('mem_type', $member['mem_type']);

  
   }
   else
   {
    //clear session data completely
    $this->session->sess_destroy();
    $error = 'some error of some sort';
  
    //HOW DO I PASS THE ERROR MESSAGE ABOVE TO THE CONTROLLER SO THAT IT CAN BE PASSED ON TO A VIEW?

   }
}

}

and the controller code is:-

Code:
<?php


class Login extends CI_Controller {

function index()

{


$this->load->model('logincontrol');
$this->logincontrol->checkmatch();

redirect(index_page());
//I NEED TO SEND THE ERROR MESSAGE TO THE VIEW CREATED BY THE DEFAULT INDEX CONTROLLER ON MY HOME PAGE OR AT LEAST BE ABLE TO PASS IT TO THIS CONTROLLER.


}






}
?>

Put simply, what is the easiest way/syntax to pass a simple variable from model to controller?

Thanks

Andy
#2

[eluser]Samus[/eluser]
[quote author="Dandy_andy" date="1337552321"]
Put simply, what is the easiest way/syntax to pass a simple variable from model to controller?

Thanks

Andy
[/quote]

return it.

Code:
function checkmatch()
  
{
  //select from database field names
     $this -> db -> select('mem_id, username, password, credits, mem_type');
  //from database table
     $this -> db -> from('members');
  //comparison (in this case where username and password matches the POSTED username and password value from the form
     $this -> db -> where('username = ' . "'" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'");
     //limit number of return rows to 1
  $this -> db -> limit(1);
    
  $usernamequery = $this -> db -> get();
   //logic control
    if($usernamequery -> num_rows() == 1)
   {
    //extract information from the relevant row in the database and convert to variable
    $member = $usernamequery->row_array();
    
    //set session data
    $this->session->set_userdata('mem_id', $member['mem_id']);
    $this->session->set_userdata('username', $member['username']);
    $this->session->set_userdata('credits', $member['credits']);
    $this->session->set_userdata('mem_type', $member['mem_type']);

  
   }
   else
   {
    //clear session data completely
    $this->session->sess_destroy();
    return $error = 'some error of some sort';
  
    //HOW DO I PASS THE ERROR MESSAGE ABOVE TO THE CONTROLLER SO THAT IT CAN BE PASSED ON TO A VIEW?

   }
}

}

Code:
<?php


class Login extends CI_Controller {

function index()

{


$this->load->model('logincontrol');
$variable = $this->logincontrol->checkmatch(); // contains $error from model if if() condition fails

redirect(index_page());
//I NEED TO SEND THE ERROR MESSAGE TO THE VIEW CREATED BY THE DEFAULT INDEX CONTROLLER ON MY HOME PAGE OR AT LEAST BE ABLE TO PASS IT TO THIS CONTROLLER.


}


}
?>
#3

[eluser]Dandy_andy[/eluser]
Thanks. I tried returning it and tested to see if the controller has it, but if I try to echo the $error from the controller (just to test it exists as a string), I get an error message saying $error is an undefined variable?
#4

[eluser]Samus[/eluser]
[quote author="Dandy_andy" date="1337585451"]Thanks. I tried returning it and tested to see if the controller has it, but if I try to echo the $error from the controller (just to test it exists as a string), I get an error message saying $error is an undefined variable?[/quote]

Yes that's correct. Because you haven't define $error in your controller.

Try echo '$variable' and see what happens Wink
#5

[eluser]Dandy_andy[/eluser]
Ah, thanks! Ok that works better now! Message is passed to controller ok, but how do I then pass the message to the view? The controller that you can see above is accessed directly from a login form submit which is separate to the controller that builds the views in the page and through which $data is passed to various parts of it. At the moment, the login controller has the message ok, but passing it to the main index page controller is my next challenge!
#6

[eluser]Samus[/eluser]
[quote author="Dandy_andy" date="1337588890"]Ah, thanks! Ok that works better now! Message is passed to controller ok, but how do I then pass the message to the view? The controller that you can see above is accessed directly from a login form submit which is separate to the controller that builds the views in the page and through which $data is passed to various parts of it. At the moment, the login controller has the message ok, but passing it to the main index page controller is my next challenge![/quote]
I think you need to restructure your controllers.

You have a controller with a public method that does not output a view? what?
#7

[eluser]Dandy_andy[/eluser]
I have a login form that is accessed from within a page as a drop down menu. When the form is submitted, it points to my Login controller which calls the checkmatch() function contained in a model. This function sets the session data and returns the results, but returns them to the Login controller. This Login controller does not control any views as the login form itself is embedded within views that are constructed through another controller. Something like....

Main controller: HOME which loads the views for the main page (header, footer etc.). The header contains the login form which is submitted to a LOGIN controller.

LOGIN controller executes the checkmatch() function which sets session data and now returns the error message if error is present. After results returned, the LOGIN controller calls a redirect the main index page which is controlled by the main controller. This is where the issue is as I need the results from the checkmatch() function to pass to the main controller through the redirect. At the moment, they are passed to the LOGIN controller only.

Does that makes sense or is there an easier way to do this?

I appreciate your help on this as it is fairly new to me but I'm enjoying making mistakes on the way!
#8

[eluser]Samus[/eluser]
Yep, what I would suggest you do is move all your current login controllers and functionality to ONE library.

You can read about creating libaries here: http://ellislab.com/codeigniter/user-gui...aries.html

Then what you would do is have your form submit to the current page, within that controller you check to see if that form has been submitted.

e.g

Code:
if($this->input->post('password')) {
     // call login library method
}
From there your library method will access a model that will call checkmatch() and return a value to your controller.

Simple?
#9

[eluser]Dandy_andy[/eluser]
Not for me! lol I have just been reading up about libaries and going through some tutorials but I can't seem to get anything to work. I have created a library class called Loginclass which is called up directly from the main homepage controller. That all works fine, but when I call up the function from the controller, I get error messages saying:-

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Loginclass::$db

Filename: libraries/Loginclass.php

Line Number: 9

Fatal error: Call to a member function select() on a non-object in C:\xampp\testsite1\application\libraries\Loginclass.php on line 9

--- wiping the sweat away from my forehead ---
#10

[eluser]Samus[/eluser]
[quote author="Dandy_andy" date="1337597144"]Not for me! lol I have just been reading up about libaries and going through some tutorials but I can't seem to get anything to work. I have created a library class called Loginclass which is called up directly from the main homepage controller. That all works fine, but when I call up the function from the controller, I get error messages saying:-

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Loginclass::$db

Filename: libraries/Loginclass.php

Line Number: 9

Fatal error: Call to a member function select() on a non-object in C:\xampp\testsite1\application\libraries\Loginclass.php on line 9

--- wiping the sweat away from my forehead ---[/quote]
Yeah because you need to include all the CI resources into it.

e.g

If you want to call a model from a library, you need to assign CI's super object into a variable.

Code:
$CI =& get_instance();
Good idea is to put that in your libraries constructor.

Then when calling a model from within the library you'd do..

Code:
$CI->load->model('model_name');
Essentially instead of using $this-> you'd now use $CI->

Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB