我的控制器是
$data['qna'] = $this->Ivote_model->get_qa();
视图
foreach($qna as $k=>$v){
echo $v['question']. '<br/>';
echo '-' .$v['answer'] . '<br/>';
模型
function get_qa(){
$data = array();
$this->db->select('*');
$this->db->where('v_questions.id',1);
$this->db->from('v_answers');
$this->db->join('v_questions','v_questions.id = v_answers.question_id');
$q = $this->db->get();
if($q->num_rows > 0){
foreach($q->result_array() as $row){
$data[] = $row;
}
}
$q->free_result();
return $data;
}
我的html页面显示
What is your favorite food?
-Sushi
What is your favorite food?
-Burgers
What is your favorite food?
-kodo
我想要的是
What is your favorite food?
-Sushi
-Burgers
-koddo
请帮助我如何实现这一目标?
答案 0 :(得分:1)
我会做几件事:
function get_qa(){
/* ... */
if($q->num_rows > 0){
foreach($q->result_array() as $row){
// create an associative array of question => answer.
// that ensures 1-1 mapping.
if(!isset($data[$row['question']])) $data[$row['question']] = array();
$data[$row['question']][] = $row['answer'];
}
}
/* ... */
}
然后,在视图中:
foreach($qna as $k=>$v){
echo $k. '<br/>';
foreach( $v as $ans ){
echo '-' .$ans . '<br/>';
}
}