我正在尝试做练习。在视图页面中,必须显示每个问题和相应的选项。我能够获得所有问题而不是选项。我想我需要传递每个问题ID以显示相应的选项。但我无法做到这一点。根据问题,有人可以帮助我们选择。
控制器代码:
function index() {
$data['qn'] = $this->questions_model->questions_list();
foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);
$data['qq_id'] = $data['qn']['$qs_id'];
//print_r($data['qq_id']);
}
$this->load->view($this->session->userdata('web_view') . '/header', $data);
$this->load->view($this->session->userdata('web_view') . '/questions_list', $data);
$this->load->view($this->session->userdata('web_view') . '/footer', $data);
}
型号代码:
function questions_list() {
$this->db->select('qbank.*,question_category.category_name');
$this->db->from('qbank');
$this->db->join('question_category','qbank.cid = question_category.cid');
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('qid',$qs_id);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
}
}
查看代码:
<form id="profilefrm" class="sky-form" name="profilefrm" method="post">
<ol>
<?php foreach($qn as $row){ ?>
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php foreach($qq_id as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>
<?php } ?>
</ol>
</form>
答案 0 :(得分:2)
控制器
'foreach($data['qn'] as $row){
$qs_id = $row->qid;
$data['qn']['$qs_id']= $this->questions_model->option_list($qs_id);'
模型
'function option_list($qs_id){
$this->db->select('option_value');
$this->db->from('q_options');
$this->db->where('q_id','$qs_id');
$this->db->limit(4);
$query = $this->db->get();
if ($query->num_rows() >= 1) {
return $query->result();
} else {
return false;
} '
查看
<li>
<?php echo $row->question; ?>
<ol type="A">
<?php
$qs_id = $row->qid;
foreach($row['$qs_id'] as $rows){ ?>
<li><?php echo $rows->option_value; ?></li>
<?php } ?>
</ol>
</li>'