Part of the EllisLab Network
   
 
Passing parameters from a view to a model
Posted: 03 June 2008 11:40 AM   [ 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 01:56 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
RankRankRank
Total Posts:  407
Joined  05-30-2008

You do that in the controller.

function yourFunction()
{
  $this
->db-where('field1', 'value');
  
$data['dada'] = $this->get('table_name');  
  
$this->load->view('your_view', $data');
}
Profile
 
 
Posted: 04 June 2008 07: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 09:05 AM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  596
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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 10:15 AM
Total Registered Members: 119534 Total Logged-in Users: 51
Total Topics: 125766 Total Anonymous Users: 5
Total Replies: 661786 Total Guests: 503
Total Posts: 787552    
Members ( View Memberlist )