Pass a variable defined in a controller to a model under CodeIgniter

I defined a variable in a method inside controller e.g.

class test extends CI_Controller {
    function tester() {
    $variable = 'value'
    }
}

Now I want to call this variable in my model. How is that possible?

Edit: I use CodeIgniter.

Answers


Model:

class your_model extends CI_Model {
    var $variable;

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

    function set_variable($variable) {
        $this->variable = $variable;
    }

}  

Controller:

class test extends CI_Controller {
    function tester() {
        $this->load->model('your_model');
        $variable = 'value'
        $this->your_model->set_variable($variable); 
    }
}

FYI - if you need a variable available for more then one method in a controller and/or in your model - you can set it in the 'constructor' of the class. Use $this-> before the variable name.

 class Test extends CI_Controller {

 public function __construct() {

 parent::__construct();

  // Set var in construct  
  $this->variable123 = 'some value 123' ;

} // end construct 

You can now call $this->variable123 from anywhere in the class and it will be available. If you load a model from this class, it will be available to any method in the model.


Need Your Help

Is there a shortcut to remove all Log statements from an Android project made in Eclipse?

android eclipse

I want to remove all the Log statements that I had added when I was making my android project. This is because I am now shipping the final project to my client.

500 server error on django site after shifting to server

django configuration apache2 mod-wsgi django-1.4

I was using Django site on local machine with ubuntu and have just uploaded but getting 500 internal server error and I am unable to understand the reason. Seems like it is unable to see some modul...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.