This is really boggling my mind on how to do this in ActiveRecord Queries in CodeIgniter. Maybe I'm over complicating it.
I have three tables: (to keep it simple I'll only show the relevant fields)
Articles (id, title, text, author) Comments (id, article_id, text, author) Users (user_id, user_type, first_name, last_name, email, password)
In the 'Articles' table, because an author can have multiple articles, I only store the authors 'user_id' from the 'Users' table, and I do the same for the comments table.
My problem is when I run an ActiveRecord query in the articles_model to get all articles along with that articles associated comments, and then echo the author in my view with $row->author, I only get the authors user_id which is 1, 2, 3, etc etc.
How do I go into the users table (different table) in that same query and get the authors actual first_name and last_name and return that?
Here's my code: In the main controller, I call a function 'getArticles'.
Controller: $data['articles'] = $this->getArticles(); $this->load->view('articles_view', $data);
function getArticles() { $this->load->model('articles_model'); $articles = $this->articles_model->getAllArticles(); return $articles; }
articles_model: function getAllArticles() { $this->db->where('id', $this->uri->segment(3)); $query = $this->db->get('articles'); return $query; }
articles_view: result() as $row) { ?> author ?> text ?>
I have a ton of other fields, but this is an extremely stripped down version for simplicity sake.
If I echo the above $row->author, I'm only going to get the user_id. I need to get the users name from the 'users' table instead. Surely I don't have to do another separate function call and pass more information into the view. Any help here would be greatly appreciated and would actually open up a world for me :)
Thanks.