1

我正在尝试使用 CodeIgniter 将两个表连接在一起。我使用 CodeIgniter 用户指南寻求帮助。我遇到了一些问题,即只显示一个表的数据,我不知道为什么。有人可以帮我吗?

这是我的代码:

控制器

function getall(){      
    $this->load->model('result_model');
    $data['query'] =
    $this->result_model->result_getall();
    $this->load->view('result_view', $data);
    }

模型

 function result_getall(){

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

    }

看法

    <div>    


 <?php foreach ($query as $row): ?>      
                     //tblanswers
                   <?php echo $row->answerA;?><br>
               <?php echo $row->answerB;?><br>
               <?php echo $row->answerC;?><br>
                   <?php echo $row->comment;?><br>
                  //credentials
                    <?php echo $row->name; ?>
         <?php endforeach; ?>  


   </div>
4

2 回答 2

2
function result_getall(){

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

}
于 2013-03-04T13:38:56.263 回答
2

在控制器中试试这个,看看结果如何。另外,如果你遇到任何错误,请告诉我们,并确保你的表中有数据:)。

控制器

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);
    }
于 2013-03-04T17:46:04.633 回答