CodeIgniter与数据库选择:

时间:2017-04-28 13:44:40

标签: php mysql codeigniter

我有两张表questionsanswers。表questions中有两列idque。表answers列中包含idque_id,选项1,optoin2 option3option4。我想打印问题,然后选择它。

$this->db->select('*');
$this->db->from('questions');
$this->db->join('answers', 'answers.que_id = questions.id', 'left');
$query = $this->db->get();

在这段代码之后,我用它的选项打印了4次问题。

2 个答案:

答案 0 :(得分:1)

嗯,在这种情况下,join方法不会帮助你。

试试这个:

$questions = $this->db->select('*')->from('questions')->get()->result();

foreach ($questions as &$question) {
    $question->answers = $this->db->select('*')->from('answers')->where('que_id', $question->id)->get()->result();
}

现在,您可以浏览$ questions对象并获得相应的答案。

希望我能帮助一点。

答案 1 :(得分:1)

试试这种方式

$this->db->select('answers.option1,answers.option2,answers.option3,answers.option4,questions.que');
$this->db->from('answers');
$this->db->join('questions', 'answers.que_id = questions.id');
$query = $this->db->get();
相关问题