我有两个表如下图:
/* 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 |
| ------ | --------- | --------------- |
| ... | | |
如何从以下两个表中进行选择:
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 } ?>
1条答案
按热度按时间eblbsuwk1#
您需要首先选择问题,然后为每个问题ID选择其选项。下面是一个示例:
然后,在视图中,您可以将问题显示为: