how do i get only one question and related answer in codeigniter way?
my controller is
$data['qna'] = $this->Ivote_model->get_qa();
view
foreach($qna as $k=>$v){
echo $v['question']. '<br/>';
echo '-' .$v['answer'] . '<br/>';
model
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;
}
my html page shows
What is your favorite food?
-Sushi
What is your favorite food?
-Burgers
What is your favorite food?
-kodo
what i want is
What is your favorite food?
开发者_Go百科-Sushi
-Burgers
-koddo
please help me how do i achieve that ?
I would do a couple of things:
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'];
}
}
/* ... */
}
Then, in the view:
foreach($qna as $k=>$v){
echo $k. '<br/>';
foreach( $v as $ans ){
echo '-' .$ans . '<br/>';
}
}
精彩评论