Querying 2 tables in 1 functon - 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: Querying 2 tables in 1 functon (/showthread.php?tid=60112) |
Querying 2 tables in 1 functon - El Forum - 01-17-2014 [eluser]comyou[/eluser] Hello all, I don't know if this is possible because I've tried but keep getting errors. Basically my view file needs some information: my actual function needs more than these but this is just an example. $username $role My function needs to query a table first 'agents' and pull the data from 'id', 'user_id' and 'role'. I then need to query a second table 'users' and pull the data 'email' and 'username' where 'id' is equal to 'user_id' from the first table. I tried having 2 queries in the function but was getting errors. Any ideas? Thanks. CODE SNIPPETS controller Code: function index() { view file Code: <ul id="the-articles" class="-list"> Querying 2 tables in 1 functon - El Forum - 01-17-2014 [eluser]Tpojka[/eluser] [quote author="comyou" date="1389996342"] I tried having 2 queries in the function but was getting errors. [/quote] Just try to put yourself in our position: there is insufficient data provided. Quote:Any ideas?Post your code snippet in code tags and maybe someone notice what is wrong. Quote: Thanks.Np. Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]comyou[/eluser] Apologies, I thought I had edited the original post with code snippets but apparently I didn't click post :p Edited original with the code which is obviously completely wrong. Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]Tpojka[/eluser] [off_topic]Why would you give some (semi-) reserved names to your functions and variables (ie get, get_where...)?[/off_topic] Seems your get() function needs one parameter and you give two parameter to it. Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]comyou[/eluser] [quote author="Tpojka" date="1390044158"][off_topic]Why would you give some (semi-) reserved names to your functions and variables (ie get, get_where...)?[/off_topic][/quote] I downloaded some models for a HMVC structure and that's what they were called so I haven't changed them lol. No specific reason. [quote author="Tpojka" date="1390044158"]Seems your get() function needs one parameter and you give two parameter to it.[/quote] You are correct, I put it in the wrong place. It was supposed to be in get() Code: function get($order_by) { Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]Tpojka[/eluser] What says the error message you're getting? Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]CroNiX[/eluser] You would use a join. I'll show the concept and you can go from there...You can fine tune with db::where(), and an additional join on your agent table, grab additional fields, etc Users -id -role_id -name Roles -id -name Code: //get all user names and email from user table, and their role_name from roles table Querying 2 tables in 1 functon - El Forum - 01-18-2014 [eluser]comyou[/eluser] [quote author="CroNiX" date="1390069688"]You would use a join. I'll show the concept and you can go from there...You can fine tune with db::where(), and an additional join on your agent table, grab additional fields, etc Users -id -role_id -name Roles -id -name Code: //get all user names and email from user table, and their role_name from roles table Thanks, never thought of using join. Sorted it right out, had to use $this->db>query though because it wasn't working using the ->select->join->get, but that's no issue. +1 internet points for you sir. |