Question

I am new to both PHP and Codeigniter.

I am loading a view from my controller. This view then has a form_open in it that directs to a function within that controller. Can I use variables previously set in my controller within that function?

For example, the constructor of the controller loads a model. Then, another function in this controller calls $this->model->someFunction($id) and sets the $id variable of my model to $id. Later, after a link is clicked in my view, it goes to a different function in the controller which then calls $this-model->printID(). This fails because in my model, $id is not set.

How can I achieve that a link goes to a function that accesses the same object (or model) that I modified earlier? That is, the final echo of $ID returns a blank string - the $ID is not set as I would have expected it to be.

Thanks in advance for the help.

My controller:

class Studio extends CI_Controller { 

public function __construct()
{
    parent::__construct();
    $this->load->model('studio_model','',TRUE);
}

function view($id)
{
    $this->studio_model->setID($id);
    $this->load->view('studio_view');
}

function signup(){                  
    $ID = $this->input->post('ID');
    $this->studio_model->signup($ID);
}
}

My model:

Class studio_model extends CI_Model{

public $ID;

function setID($id) {
    $this->ID = $id;
}

function signup($ID){
    echo $this->ID;
    echo $classID;
}
Était-ce utile?

La solution

I think that the answer to my question is related to the following: ellislab.com/forums/viewthread/185409/#876547 A PHP instance is not maintained and so each new call instantiates new classes, controllers, etc. As a result, global variables are no longer set if you have left a controller and then re-enter it later.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top