CodeIgniter Forums
Query from view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Query from view (/showthread.php?tid=26525)



Query from view - El Forum - 01-15-2010

[eluser]greenba[/eluser]
Hello,

I have query that runs in the controller:
Code:
$data['query'] = $this->Member->select_sql($id);    
    $this->load->view('myform');

and then outputs data in the view:

Code:
foreach ($query->result() as $row):
   echo $row->post_title;
   echo $row->post_user_id;
endforeach;

So this outputs me a list of posts made by a user. Now I would like to run one more query that would for each post loop through my user table and output user information next to each post. (I dont want to select data from a view or joint those 2 tables at this time in MySQL)

Any ideas?


Query from view - El Forum - 01-15-2010

[eluser]jamziee[/eluser]
Is the list based around one user?


Query from view - El Forum - 01-15-2010

[eluser]greenba[/eluser]
No. there are many users.


Query from view - El Forum - 01-15-2010

[eluser]jamziee[/eluser]
dose the information about the user haft to be on the same page?


Query from view - El Forum - 01-15-2010

[eluser]greenba[/eluser]
Yes it does.


Query from view - El Forum - 01-15-2010

[eluser]jamziee[/eluser]
you select all the users from you database. pass it through to the view and then select which one you want for each post, this could be a bit uneficiant though. But work


Query from view - El Forum - 01-15-2010

[eluser]Colin Williams[/eluser]
1. Use a JOIN query to get all the data you need.
2. In the model after the query, iterate over the result, grouping multiple values. You should end up with a multidimensional array of posts, where each post has a user property that is an object containing the user data
3. Pass this on to the view

This is a basic workflow. If you don't know how joins work, or how to manipulate arrays, I would suggest you go back to square one and do some reading on each of those topics.