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();
}
}
6
  • Check stackoverflow.com/questions/17013397/… and stackoverflow.com/questions/19237316/… Commented Jun 22, 2016 at 10:47
  • there are assign value inside constructor. i assigned inside index(). even i don't want super global variable. Commented Jun 22, 2016 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. Commented Jun 22, 2016 at 10:54
  • fist my index function called. after that others. Commented Jun 22, 2016 at 10:56
  • You can pass it like $this->about($no) call inside index function Commented Jun 22, 2016 at 10:59

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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
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 .

Comments

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();
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.