Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

join query in CodeIgniter [duplicate]

I am using join query in CodeIgniter and can't get it to work. It only displays one table data, but not the other. I am new to CodeIgniter and can't figure this out. Pleas someone help me. Tnanks in advance.

view

<?php foreach ($query as $row): ?>

    <?php echo $row->answerA;?><br>
    <?php echo $row->answerB;?><br>
    <?php echo $row->answerC;?><br>
    <?php echo $row->comment;?><br>
    <?php echo $row->name; ?>

<?php endforeach; ?>  

controller

function getall() {

    $this->load->model('result_model');
    $data['query'] = $this->result_model->result_getall();
    // print_r($data['query']);
    // die();
    $this->load->view('result_view', $data);

}

model

function result_getall() {

    $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'right outer'); 
    $query = $this->db->get();
    return $query->result();

}

EDIT

The result of

print_r($data['query']);
die();

is an array as below:

Array ( [0] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 83 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => [email protected] ) [1] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 84 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => [email protected] ) [2] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 85 [name] => Edvinas [second_name] => Liutvaitis [phone] => [email] => [email protected] ) [3] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 86 [name] => EdvinasQ [second_name] => LiutvaitisQ [phone] => 12345678 [email] => [email protected] ) [4] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 87 [name] => Edvinas [second_name] => Liutvaitis [phone] => 123456 [email] => [email protected] ) ) 

table structure

credentials

cid(PRIMARY), name, second_name, phone, email

tblanswers

answerid(PRIMARY), userid, questionid, answerA, answerB, answerC.

like image 922
Edvinas Liutvaitis Avatar asked Mar 05 '13 07:03

Edvinas Liutvaitis


2 Answers

Try with this:

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid');

Or

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'inner');

And print the result to see if is what you want

like image 177
Alessandro Minoccheri Avatar answered Oct 21 '22 04:10

Alessandro Minoccheri


What's the primary table you want to query? Well you can try this one, suggested that when using joins, you must specify an alias for each table:

 function result_getall(){

    // if you want to query your primary data from the table 'tblanswers',
    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('tblanswers a');
    $this->db->join('credentials b', 'b.cid = a.answerid', 'left'); 
    $query = $this->db->get();
    return $query->result();

    // if you want to query your primary data from the table 'credentials',

    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('credentials a');
    $this->db->join('tblanswers b', 'b.answerid = a.cid', 'left'); 
    $query = $this->db->get();
    return $query->result();

}
like image 30
PinoyPal Avatar answered Oct 21 '22 06:10

PinoyPal