Welcome Guest, Not a member yet? Register   Sign In
How am i supposed to access and change a variable over different controllers ?
#6

(11-19-2014, 12:07 PM)invader7 Wrote: Hello , im trying to solve my mysterious  Sad  problem with codeigniter !

I want to pass a value from one controller's function to another controller's function and edit it's value.

This is what i have



Code:
class MY_Controller extends Controller { }
class MY_User extends MY_Controller {}

and

class Liveshow extends MY_Controller {
    function a() {
        //set a global $test = 123;
    }
}

class Channels extends MY_User {
    function b() {
        //echo the global variable $test
    }
}


How am i supposed to make it work ?  

Thanks !



Although you can't pass a value from one Controller to another you can process the method in a model and return the result. Then access it from any controller you need to.

If you mean to have a variable from MY_Controller and assign a value from any controller that extends MY_Controller you can do this:
PHP Code:
class MY_Controller extends CI_Controller {

 
 public $data = array();

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

 
 public function load_view($subview$your_variable'' // could be an array here)
 
 {
 
     // $this->data allows to pass any array value to the view
 
     // like errors or values.
 
     $this->data['your_variable_here'] = $your_variable;

 
     $this->data['subview'] = $subview;
 
     $this->load->view('layouts/layout'$this->data);
 
 }


I use the $subview variable to pass the view of a controller using a layout.
Reply


Messages In This Thread
RE: How am i supposed to access and change a variable over different controllers ? - by efenollal - 11-20-2014, 08:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB