Get data from two tables in single foreach - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Get data from two tables in single foreach (/showthread.php?tid=71208) |
Get data from two tables in single foreach - kirasiris - 07-19-2018 So I currently have three tables: AI = auto increment. ci_comments: comment_id(AI), email, website, body. ci_users: user_id(AI), username, slug, biography. ci_relationship: id(AI), user_id, comment_id. And I'm trying to display all the comment that have been made in the website by the users. In order to display them I have to do a foreach which returns a $query->result(); as I'm a big lover of the '->'s and works fine but I'm as well trying to display the user_id who created the comment which stored in a different table, the problem is how do I do it in the same loop? This is my controller method: PHP Code: public function index() This is the method that I use in the model to get data from the table ci_comments: PHP Code: public function get_list() and this is the what that I'm trying to create/modify/figure to get the comment_id and user_id data from the ci_relationship table in the same foreach in which I display the comments: PHP Code: public function get_author(){ and this is my view: PHP Code: <?php foreach($comments as $page) : ?> Thanks in advance. RE: Get data from two tables in single foreach - Pertti - 07-19-2018 Because comment is only made by one user, I think it wouldn't be wrong to move user_id itself to comment, however this is what you could do with -> PHP Code: $query = $this->db RE: Get data from two tables in single foreach - php_rocs - 07-19-2018 @kirasiris, Another suggestion would be to take the query suggested by Pertti and make it into a database view. Then all you would have to do is do basic call to the database view and then push the results to the display view. |