Welcome Guest, Not a member yet? Register   Sign In
Querying 2 tables in 1 functon
#1

[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
$email
$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() {
            $query_freeagent = $this->get('id')->result_array();
            foreach($query_freeagent as $row) :
                $user_id = $row->user_id;
                $data = array (
                    $query_user = $this->get_where($user_id),
                );
            endforeach;
            $data = array (
                'freeagent_records' => $query_freeagent,
                  'user_records' =>$query_user,
            );
           $this->load->view('freeagents/index', $data);
        }
    function get($order_by) {
        $table = "freeagents";
        $this->db->order_by($order_by, 'desc');
        $query=$this->db->get($table);
        return $query;
    }
    
    function get_where($id) {
        $table = "users";
        $this->db->where("id", $id);
        $query=$this->db->get($table);
        return $query;
    }

view file
Code:
<ul id="the-articles" class="-list">

    &lt;?php foreach($freeagent_records as $row):
                                    $user_id = $row['user_id'];
                                    $gt= $row['gamertag'];
                                    $console = $row['console'];
                                    $role = $row['role'];
                                    $game = $row['game'];
                                    $twitter = $row['twitter'];
                                foreach($user_records as $row_2):
                                    $user_id = $row_2->user_id;  
                                ?&gt;
                <li>
                    <h3>&lt;?php echo $gt; ?&gt; </h3>
                                            <span class="meta">
                                                ID: <b>&lt;?php echo $user_id; ?&gt;</b>
                                                Console: <b>&lt;?php echo $console; ?&gt;</b>
                                                Role: <b>&lt;?php echo $role; ?&gt;</b>
                                                Game: <b>&lt;?php echo $game; ?&gt; </b>
                                            </span>
                                            
     </li>
    &lt;?php endforeach;  
                                endforeach; ?&gt;
                
            </ul>


Messages In This Thread
Querying 2 tables in 1 functon - by El Forum - 01-17-2014, 03:05 PM
Querying 2 tables in 1 functon - by El Forum - 01-17-2014, 06:30 PM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 03:01 AM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 04:22 AM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 04:35 AM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 05:30 AM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 11:28 AM
Querying 2 tables in 1 functon - by El Forum - 01-18-2014, 02:15 PM



Theme © iAndrew 2016 - Forum software by © MyBB