Part of the EllisLab Network
   
 
Passing parameters from a view to a model
Posted: 03 June 2008 01:40 PM   [ Ignore ]  
Summer Student
Total Posts:  2
Joined  09-14-2007

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?

Profile
 
 
Posted: 03 June 2008 03:56 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  499
Joined  05-30-2008

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’);
}
[/Code]

Profile
 
 
Posted: 04 June 2008 09:59 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  2
Joined  09-14-2007

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

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…?

Profile
 
 
Posted: 04 June 2008 11:05 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  593
Joined  02-04-2008

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.

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

Profile