Welcome Guest, Not a member yet? Register   Sign In
Passing parameters from a view to a model
#1

[eluser]Unknown[/eluser]
Hi
Been using CI on and off for almost a year now. Like a lot of the features but I still find myself puzzeled quite often.

I have a loop in a view:

foreach($dada->result() as $didi):

In here I would like to make a call to a database (through the controller and model) but how do I pass the current $didi->id to the model?
(Could ofcourse just $this->db->query('SELECT * FROM dudu WHERE id = ' . $didi->id) directly but...)

endforeach;

Can anyone help?
#2

[eluser]stuffradio[/eluser]
You do that in the controller.

Code:
function yourFunction()
{
  $this->db-where('field1', 'value');
  $data['dada'] = $this->get('table_name');  
  $this->load->view('your_view', $data');
}
#3

[eluser]Unknown[/eluser]
Thanks for your answer. I still don't understand it complety though...

How do I tell the controller what value to pass to field1 from the view?

Say I have a for loop in my view
Code:
for($i=1; $i<123;$i++){
$dada = $this->db->query('SELECT * FROM ijij Where id = ' . $i);
//Thats what I want to do from the controller/model
}
how would I do that from the controller? Is this a job for the helpers or...?
#4

[eluser]louis w[/eluser]
You just want to call a method in your model with an id from your view? Pretty sure you can call the model directly without going through your controller. I could be mistaken thou as I have never tried this. I normally get all my data before handing off to a view.

Code:
// IN CONTROLLER:
$this->load->model('my_model');


// MODEL (my_model)
public function getByID($id) {
   return $this->db->where('id', $id);
}

// IN VIEW:
$data = $this->my_model->getByID($id);

Is this what you are trying to do? Maybe i am misunderstanding.




Theme © iAndrew 2016 - Forum software by © MyBB