• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codigniter grouping in views

#1
Big Grin 
Hello Codeigniter Community.

I am just new to Codeigniter. And I am facing a small challenge now. Would you help me solve this?  Smile Smile

Here is my problem:

I am making a questionaire system. The user should be able to create a questionaire, add questions and add choices to it.

The question will go to "question" table and the choices will go to the "choices" table.

I did this by saving the question first, get the inserted id, then insert each choice.

Now, I want to retrieve all the questions connected to a questionaire. I did this by joining the question table and the choices table.

I want my questions to look like this:

EXAMPLE
-------------------------------
1. How's your mama?
[] Happy
[] Not Happy
-------------------------------------

Now my question is:

How can I display the question once, then display all the choices below? Like the provided example above.

Here's what I have tried so far:

Code:
    public function select_question_choices($survey_id)
    {
        $this->db->select('question.id as question_id');
        $this->db->select('question.title as question_title');
        $this->db->select('choices.title as choices_title');
        $this->db->from('question');
        $this->db->join('choices', 'question.id = choices.question_id');
        $this->db->where(['question.survey_id' => $survey_id]);
        $query = $this->db->get();

        return $query->result_array();
    }

Then I call that method in a controller. Then I pass the result into a view.

Now, when I do a foreach loop, the result I get, compared to the given example above:

How's your mama
[] Happy

How's your mama
[] Not Happy

It shows the quesiton as many times as the choices. So if I have 5 choices, it will also display the question 5 times. 

Would you help me with this? I hope I explained myself well. Thanks in advance. Any help/suggestion would be much appreciated.  Smile Smile
Reply


Messages In This Thread
Codigniter grouping in views - by danangeloalcanar - 10-28-2017, 07:34 AM
RE: Codigniter grouping in views - by PaulD - 10-28-2017, 07:56 AM
RE: Codigniter grouping in views - by PaulD - 10-29-2017, 04:27 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.