I have such function in model.
function news()
{
$data = array(
'title' => $this->input->post('title'),
'date' => $this->input->post('date'),
'newstext' => $this->input->post('newstext'),
);
$this->db->insert('news', $data);
}
And I want use this $data['title']
in another function inside the same model. How can I do that?
The easiest way to accomplish that:
Declare a global variable right after the Model Class definition.
e.g.
class ModalName extends CI_Model()
{
public $title;
function news()
{
$data = array(
'title' => $this->input->post('title'),
'date' => $this->input->post('date'),
'newstext' => $this->input->post('newstext'),
);
$this->db->insert('news', $data);
$this->title = $data['title'];
}
}
Then,
$this->title
will be available in every function inside your Model Class.
First I think is better to stores the input values in controller instead of the model to follow MVC pattern so you have only database related operations in your model, then, you can call that and the other function from the controller passing to both functions the same input values that you stored in your controller.