CodeIgniter Forums
relational DB - 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: relational DB (/showthread.php?tid=43405)



relational DB - El Forum - 07-10-2011

[eluser]matches[/eluser]
Can someone point me to a good relational DB example using codeigniter? I have a blog table and a blog_categories table related to it. I'm just not sure how to display each blog entry with it's related category.

Thanks


relational DB - El Forum - 07-12-2011

[eluser]CroNiX[/eluser]
http://ellislab.com/codeigniter/user-guide/database/active_record.html

Check out the $this->db->join() section and example.


Or a simple one for you...

BLOG
----
-id
-title
-category_id

CATEGORIES
----------
-id
-name
Code:
$result = $this->db
  ->select('BLOG.title AS title, CATEGORIES.name AS category')
  ->join('CATEGORIES', 'CATEGORIES.id = BLOG.category_id')
  ->get('BLOG')
  ->result_array();

would give you an array of all blog titles along with its category name.