在codeigniter中加入查询 [英] join query in codeigniter

查看:87
本文介绍了在codeigniter中加入查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter中使用join函数,不能得到它的工作它只显示一个表数据,但不是其他新的codeigniter没有想出这个请求有人帮助我提前tnx。



查看



 <?php foreach ):δ> 

<?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; >

控制器

  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);

}

模型

  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

这是

的结果

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

它返回的数组:

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

表结构



凭证



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



tblanswers



解决方案

尝试使用:

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

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

并打印结果,看看是否是您想要的


i am using join function in codeigniter and cant get it to work it only displays one table data but not the other am new to codeigniter cant figure this out pleas someone help me tnx 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

this is the result of

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

the array that it returns:

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

table structure

credentials

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

tblanswers

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

解决方案

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

这篇关于在codeigniter中加入查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆