Trying to join tables to display the author of blog entry - 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: Trying to join tables to display the author of blog entry (/showthread.php?tid=13930) |
Trying to join tables to display the author of blog entry - El Forum - 12-11-2008 [eluser]Brad Morse[/eluser] I am trying to add more to the blog tutorial. Here is the blog view: Code: <?php foreach($query->result() as $row): ?> It displays the author id for now (entries.author_id = authors.id), I want it to display the author first (authors.first_name) and last name (authors.last_name). Could you please help me? Here is the blog controller: Code: function index() { Trying to join tables to display the author of blog entry - El Forum - 12-11-2008 [eluser]missionsix[/eluser] well, since i'm not an active record db guy, i'll just give you the sql used: Code: $SQL = "SELECT blog_entries.*, users.* FROM blog_entries I hope you know how to read SQl. The two tables are blog_entries & users, i think you can figure it out though. Trying to join tables to display the author of blog entry - El Forum - 12-11-2008 [eluser]Brad Morse[/eluser] This is what I came up with with a little digging thru the forums: Please let me know if there is a better way to do this: Code: Controller Trying to join tables to display the author of blog entry - El Forum - 12-11-2008 [eluser]crumpet[/eluser] i like to add $this->db->join('authors', 'authors.id = entries.author_id', 'inner'); so that if the user account gets deleted then the blog entry will not appear this is a matter of taste however - maybe you want the blog entry to still appear if the user account is deleted. if so you will need to have error checking so you dont get Undefined first_name error |