[SOLVED]Giant CI noob here - I can't get the data I get from the database to work with my View? |
[eluser]Unknown[/eluser]
Okay, so here's the code in my model: Code: public function getRecentProjects() Now, my controller passes $q directly onto my view, how would I then access the data from there in my view? Here's the relevant code for my controller: Code: $q = $this->projects->getRecentProjects(); Sorry for any spelling mistakes, etc, I've been up for a while trying to work it out. Cheers
[eluser]Chris Williams[/eluser]
If you apply print_r($q) to your variable you get some sense of what is accessible. Returned records from a query would be found in $q->result() Code: foreach( $q->result() as $r )
[eluser]TWP Marketing[/eluser]
[quote author="timtamboy63" date="1309212089"]Okay, so here's the code in my model: Code: public function getRecentProjects() Now, my controller passes $q directly onto my view, how would I then access the data from there in my view? Here's the relevant code for my controller: Code: $q = $this->projects->getRecentProjects(); Sorry for any spelling mistakes, etc, I've been up for a while trying to work it out. Cheers[/quote] Please note that Code: $this->db->get('projects,5); will return an OBJECT and uses the Active Record Class, read about that in the User Guide. I assume you want 5 records from the projects file? The get() function accepts two parameter here (limit and offset, see the User Guide). When you pass this to your view 'main', using $this->load->view('main',$data), the ARRAY named $data will be exploded and only the array elements will be visible in the view. Therefore, you need to extract your data from the query OBJECT and create an array ($data) to be passed to the view. Reference the User Guide: http://ellislab.com/codeigniter/user-gui...sults.html Code: $q = $this->projects->getRecentProjects(); // get the query OBJECT Code: ...
[eluser]Unknown[/eluser]
That makes a lot of sense, thanks. I tried Code: foreach( $q->result() as $project) But I couldn't access the $project object from my view, for obvious reasons. I'll give it a go now, cheers
[eluser]TWP Marketing[/eluser]
[quote author="timtamboy63" date="1309248251"]That makes a lot of sense, thanks. I tried Code: foreach( $q->result() as $project) But I couldn't access the $project object from my view, for obvious reasons. I'll give it a go now, cheers[/quote] You would need to add an array name within the data array: Code: foreach( $q->result() as $project) |
Welcome Guest, Not a member yet? Register Sign In |