如何在 Codeigniter 中的另一个 select 查询中添加一个 select 查询
How to add a select query in another select query in Codeigniter
我有两个表格,如下所示:
/* questions table */
| q_id | q_title |
| ------- | ---------------------- |
| 1 | What is your name? |
| ------- | ---------------------- |
| 2 | What is your gender? |
| ------- | ---------------------- |
| ... | |
/* options table */
| o_id | o_title | o_question_id |
| ------ | --------- | --------------- |
| 1 | George | 1 |
| ------ | --------- | --------------- |
| 2 | Sarah | 1 |
| ------ | --------- | --------------- |
| 3 | Michael | 1 |
| ------ | --------- | --------------- |
| 4 | Male | 2 |
| ------ | --------- | --------------- |
| 5 | Female | 2 |
| ------ | --------- | --------------- |
| ... | | |
如何从两个表中select如下:
1.你叫什么名字?
乔治
莎拉
迈克尔
2。你的性别是?
男
女
“JOIN”重复提问。
我的代码哪里错了?
我的控制器:
/* controller */
$data['questions'] = $this->db->get('questions_tbl')->result();
$items_arr = array();
foreach ($data['questions'] as $option) {
$items_arr[] = $this->db->where('o_question_id', $option->q_id)->get('options_tbl')->result();
}
$data['options'] = $items_arr;
我的观点:
/* view */
<?php foreach ($questions as $q) { ?>
<strong><?= $q->q_id ?>.<?= $q_title ?></strong>
<?php foreach ($options as $o) { ?>
<?php if ($o->o_question_id == $q->id) { ?>
<p><?= $o->o_title ?></p>
<?php } ?>
<?php } ?>
<?php } ?>
您需要先 select 问题,然后对于每个问题 ID,select 是选项。
下面是一个例子:
function questions(){
$qtns = array();
$query = $this->db->get('questions_table');
foreach($query->result_array() as $one){
$one['choices'] = $this->questionChoices($one['id'];
$qtns[] = $one:
}
return $qtns;
}
function questionChoices(int $qtnID){
$this->db->where('o_questions_id', $qtnID);
$query = $this->db->get('options_table');
return $query->result_array();
}
然后从您的角度来看,您可以将问题显示为:
<?php foreach($qtns as $one){ ?>
<strong><?= $one['q_id'] ?>.<?= $one['q_title'] ?></strong>
<?php foreach ($one['choices'] as $o) { ?>
<p><?= $o['o_title'] ?></p>
<?php } ?>
<?php } ?>
我有两个表格,如下所示:
/* questions table */
| q_id | q_title |
| ------- | ---------------------- |
| 1 | What is your name? |
| ------- | ---------------------- |
| 2 | What is your gender? |
| ------- | ---------------------- |
| ... | |
/* options table */
| o_id | o_title | o_question_id |
| ------ | --------- | --------------- |
| 1 | George | 1 |
| ------ | --------- | --------------- |
| 2 | Sarah | 1 |
| ------ | --------- | --------------- |
| 3 | Michael | 1 |
| ------ | --------- | --------------- |
| 4 | Male | 2 |
| ------ | --------- | --------------- |
| 5 | Female | 2 |
| ------ | --------- | --------------- |
| ... | | |
如何从两个表中select如下:
1.你叫什么名字?
乔治
莎拉
迈克尔
2。你的性别是?
男
女
“JOIN”重复提问。
我的代码哪里错了?
我的控制器:
/* controller */
$data['questions'] = $this->db->get('questions_tbl')->result();
$items_arr = array();
foreach ($data['questions'] as $option) {
$items_arr[] = $this->db->where('o_question_id', $option->q_id)->get('options_tbl')->result();
}
$data['options'] = $items_arr;
我的观点:
/* view */
<?php foreach ($questions as $q) { ?>
<strong><?= $q->q_id ?>.<?= $q_title ?></strong>
<?php foreach ($options as $o) { ?>
<?php if ($o->o_question_id == $q->id) { ?>
<p><?= $o->o_title ?></p>
<?php } ?>
<?php } ?>
<?php } ?>
您需要先 select 问题,然后对于每个问题 ID,select 是选项。 下面是一个例子:
function questions(){
$qtns = array();
$query = $this->db->get('questions_table');
foreach($query->result_array() as $one){
$one['choices'] = $this->questionChoices($one['id'];
$qtns[] = $one:
}
return $qtns;
}
function questionChoices(int $qtnID){
$this->db->where('o_questions_id', $qtnID);
$query = $this->db->get('options_table');
return $query->result_array();
}
然后从您的角度来看,您可以将问题显示为:
<?php foreach($qtns as $one){ ?>
<strong><?= $one['q_id'] ?>.<?= $one['q_title'] ?></strong>
<?php foreach ($one['choices'] as $o) { ?>
<p><?= $o['o_title'] ?></p>
<?php } ?>
<?php } ?>