• 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

#2
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
$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.
Code:
// 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,

Paul.

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

#3
Hi Paul! Smile

Thanks for your help. I am now reading your suggestion so I can try it. 

Here's my view:
Code:
<?php
if ($questions):
    $ctr = 0;
    foreach ($questions as $question):
        $ctr++;
        ?>
        <div class="panel panel-default">

            <div class="panel-heading">
                <?php echo $question['question_title']; ?>
            </div>

            <div class="panel-body">
                <?php echo $question['choices_title']; ?>
            </div>

        </div>

        <?php
    endforeach;
endif;
?>

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.  Smile Smile
Reply

#4
If you are using your original array something like this would work:

PHP Code:
<?php if ( ! empty($questions)) { ?>

    <?php $first_question TRUE?>
    <?php $current_question 0?>
    
    <?php foreach ($questions as $question) { ?>

        <!-- Is this a new question? -->
        <?php if ($question['question_id'] != $current_question) { ?>
        
            <!-- are we closing a previous question panel? -->
            <?php if ( ! $first_question) { ?>
                </div> <!-- close panel body -->
                </div> <!-- close panel -->
            <?php } else { ?>
                <?php $first_question FALSE?>
            <?php ?>
            
            <!-- set current quesiton id, so title does not show for each choice -->
            <?php $current_question $question['question_id']; ?>
            
            <!-- show current question -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <?php echo $question['question_title']; ?>
                </div>
                <div class="panel-body">

        <?php ?>
            
        <?php // show choices title ?>
        <?php echo $question['choices_title']; ?>

    <?php ?>
    
    <!-- closing last question panel -->
    </div><!-- close panel body -->
    </div><!-- close panel -->
    
<?php ?>
Reply

#5
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.   Cool Cool
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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