Codigniter grouping in views |
Hello Codeigniter Community.
I am just new to Codeigniter. And I am facing a small challenge now. Would you help me solve this? ![]() ![]() 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) 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. ![]() ![]()
There are two ways you could do this. One is with some logic in the view, the other is with additional database calls. It really depends how many questions you are going to display. I could assume you are showing one question per page view then the next question etc, but if that is not the case the answer changes.
In pseudo code you would do something like: Code: // set current question to 0 to test against However if you are just showing one question it all becomes a bit easier. Code: // set show question to 0 to test against Hope that makes sense. Alternatively when you collect the question from your db, just collect the question into a row_array. Then add a new db request for the question options as a result_array and put that into, say, $question['options']. Then you output the question and foreach loop through the options to display them all. Again, hope that makes sense, Paul. PS If you have shown your view I could have been more helpful.
Hi Paul!
![]() Thanks for your help. I am now reading your suggestion so I can try it. Here's my view: Code: <?php So basically, it will just display a boostrap panel, with the question as heading, and in the body, the choices are listed. Thanks again Paul for guiding me. I will give a feedback asap. ![]() ![]()
If you are using your original array something like this would work:
PHP Code: <?php if ( ! empty($questions)) { ?>
Hi Paul! You have given me the exact answer, thank you so much. It is a new approach to a problem, I cannot think of that solution. I can apply that to some problems too . If I can only show the screenshot of my view. It works perfectly. Thanks again for your help. Love from Philippines.
![]() ![]() |
Welcome Guest, Not a member yet? Register Sign In |