0

i have defined global variable inside controller but i am assign value inside index() function. value accessible inside index() but not inside about and other functions. how can i do it ??

class Manage_business extends CI_Controller
    {
    var $id;

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

    public function index($no)
    {

        $this->id=$no;
        echo $this->id;
    }

    public function about()
    {
        echo $this->id;
        die();
}
}
Mitul Lakhani
  • 182
  • 1
  • 12
  • Check http://stackoverflow.com/questions/17013397/code-igniter-best-place-to-declare-global-variable and http://stackoverflow.com/questions/19237316/how-to-use-global-variable-in-php-codeigniter – Saty Jun 22 '16 at 10:47
  • there are assign value inside constructor. i assigned inside index(). even i don't want super global variable. – Mitul Lakhani Jun 22 '16 at 10:53
  • If you are directly using about function then you are no able to set the value for $this->id through index function because its never called. – Ali Jun 22 '16 at 10:54
  • fist my index function called. after that others. – Mitul Lakhani Jun 22 '16 at 10:56
  • You can pass it like `$this->about($no)` call inside index function – Saty Jun 22 '16 at 10:59
  • i have 7 to 8 functions. i put only about for demo. – Mitul Lakhani Jun 22 '16 at 11:01

3 Answers3

1

Try using sessions, is one of the many options you've got

class Manage_business extends CI_Controller {

public function __construct()
{
    parent::__construct(); 
    $this->load->library('session);
}

public function index($no)
{
    $this->session->set_userdata('id',$no) //to last
    $this->session->set_flashdata('id',$no) //available only for the next refresh
    echo $this->id;
}

public function about()
{
    echo $this->session->id;
    die();

} }

this way you can see what you've done on the index, please see it that works for you, there are other method, but this one i think is the most efective and easy to use.

marcoFSN
  • 125
  • 9
  • @wolfgang1983 yeah, but is what i wrote above, he is trying to fill a global var, but in different methods, for calling those methods i guees he is refreshing the page, for that, he need sessions or anything that'll keep his data along the controller – marcoFSN Jun 22 '16 at 13:10
0

You are using var in the variable declaration, which is generating the syntax error. The global variable can be created in the construct() so that the variable can be used by other functions in the class.

Please check the corrected code below.

class Manage_business extends CI_Controller
{


public function __construct()
{
    parent::__construct(); 
    $id;        
}

public function index($no)
{

    $this->id=$no;
    echo $this->id;
}

public function about()
{
    echo $this->id;
    die();
}
}
JamieA
  • 1,984
  • 4
  • 27
  • 38
0

codeigniter does not allow this method as you should define values with in controller or modal and them forward on . one of easiest method is to define as a constant in config/constants.php this will be available through out the app without passing to modal or view or controller they will be globally accessable anywhere .. another method is using session values in cisession as you have to call session and extract values .