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

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 !
Reply
#2

(This post was last modified: 11-19-2014, 08:17 PM by bclinton.)

Controllers handle requests by users.  There is no way to share global variables across different requests.

Is this variable specific to the user? store it in a session.

Does this variable pertain to the application? store it in the DB.
Reply
#3

@invader7

Don't bloat the session. Search the Internet about what "registry pattern" is. I have got a small library for supporting such a feature. https://github.com/ivantcholakov/codeigniter-registry
Reply
#4

@ ivantcholakov

In order for your registry library to maintain values across multiple different requests it would need be a singleton, no?  Can you post a simple example of this?  Perhaps something like incrementing a simple counter in the welcome/index function?
Reply
#5

@bclinton
Registry is like a collection of global variables. It does not transport values between requests, it is what session class does.
Reply
#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
#7

I did it by saving values to database. Thank you very much for your answers !
Reply
#8

you would use the session class to do this. something like: 

Set the variable and it's value:


PHP Code:
$this->session->set_flashdata('test''1234'); 

Get the value of the variable:

PHP Code:
$test $this->session->flashdata('test'); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB