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

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:
// set current question to 0 to test against
$current_question = 0;

// set if first question or not so you can test to see if you need to display closing code for the question block
$first_question = TRUE;

// your loop through questions
foreach question... {

 // question title : test if it is a new question before showing header.
 if new question (ie question id is not = current question) then {

     // test if first question, if not show closing code for previous question
     if not first question then {
           set $first_question = FALSE;
     } else {
           display question block closing HTML (</ul></div> etc)

     display question opening block and title

 // show option
 display optional answer

// end foreach

However if you are just showing one question it all becomes a bit easier.
// set show question to 0 to test against
$show_question = 0;

// your loop through array
foreach choices... {

 // question title : test if you need to show question
 if $show_question then {

     display question
     set $show_question = FALSE;
 // show option
 display option

// end foreach

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,


PS If you have shown your view I could have been more helpful.

Messages In This Thread
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-2020 MyBB Group.