Jump to content

how do i get only one question and related answer in codeigniter way ?


linux1880

Recommended Posts

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?
    -Sushi
    -Burgers
    -koddo

 

please help me how do i achieve that ?

 

  • 2 weeks later...

Thanks a lot , it is working for single question, bot not when i have multiple questions and answers. Could you pls give me a hint what am i missing ? Thanks

 

Well if that's the case you may need some more intellegant loop logic. You could use a second loop, but that may get quite messy imo. Can you post your database structure?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.