Query multiple tables and echo the result - 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 multiple tables and echo the result (/showthread.php?tid=33756) |
Query multiple tables and echo the result - El Forum - 09-07-2010 [eluser]gmask[/eluser] Hello, I'm having a really hard time getting the results I want from the database. Here's what I'm trying to do: I have an entries view that lists all of the entries in my database. In each of these entries is a field called 'category' which points to the `categories` table. I would like to be able to have the ability to show which category my entry was posted in, but I can't seem to get it working. Here's the way I'm going about it, please correct me if I'm being silly: This is in my model Code: function entries($num, $offset) This is the relevant section of my controller Code: function entries() This is the relevant section of my view Code: <?php foreach($entries->result() as $entry) { ?> Obviously I'd like to be able to echo the category title field in the anchor, replacing the 'Parent' text. What's the best way to do this? Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]gmask[/eluser] Bump Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]bcorcoran[/eluser] Post your database tables, and I might be able to give you an answer. Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]gmask[/eluser] The `entries` table has an entry with the following structure Code: id -> '1' Code: id -> '1' Code: $entry->category Thanks! Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]bcorcoran[/eluser] Ahhh, ok. Here's a lesson in SQL: If you have two tables you need to join (in your case), and the column names are colliding (i.e. both tables have ID field and you need to use each of them separately). You want to do the following: Code: SELECT entries.id, entries.title, categories.id as cid, categories.title as ctitle Then this will be $entry->cid and $entry->ctitle for cat id and cat title respectively. Let me know if this doesn't make sense. Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]kaejiavo[/eluser] [quote author="gmask" date="1283905681"] ... This is in my model Code: function entries($num, $offset) ... This is the relevant section of my view Code: <?php foreach($entries->result() as $entry) { ?> Obviously I'd like to be able to echo the category title field in the anchor, replacing the 'Parent' text. What's the best way to do this?[/quote] 1. Change your query Code: function entries($num, $offset) entries.title will overwrite categories.title because it is defined later, so you have to alias this column. same with the id columns. 2. in your view change Code: anchor('edit_category/'.$entry->category_id, $entry->category_title) Marco Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]bcorcoran[/eluser] Haha, same response (more or less), 1 minute apart! Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]kaejiavo[/eluser] you were a bit faster Query multiple tables and echo the result - El Forum - 09-08-2010 [eluser]gmask[/eluser] You guys are awesome, thanks a lot! |