CodeIgniter Forums

Full Version: Moving site visitors through a whole bunch of 'yes/no' questions - best way to do this?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]tinawina[/eluser]
Hi - I am developing an application that moves people through a series of yes/no questions:

Question #1:
Answer: yes -> track visitor to Question #1b
Answer: no -> track visitor to Question #2

Question #1b:
Answer: yes? -> track visitor to Question #1c
Answer: no? -> track visitor to Question #3

etc.

In the end, we'll have a lot of questions -- a visitor will answer somewhere's around 25 total, start to finish. We are only using the answers to get to a definitive yes/no -- not saving all of these answers to a dbase or anything.

(All of this yes/no business is an attempt to help people understand copyright and hopefully get them to use a Creative Commons license to freely share their written works.)

Before I dig into building this out, I'm wondering what the best way to structure it is. For instance, it seems to me that I should be able to dynamically generate the question form view from the controller rather than building out lots of individual files in my view folder, one for each yes/no question. Of course, I'm scratching my head re: how to actually do this.

I appreciate any insights, ideas, theories, cautions, tips and/or tricks you can share with me! Thanks!

El Forum

[eluser]coolfactor[/eluser]
If you're not storing in a database, how are you collecting the answers?

I'd create a configuration file for each of your questions.
Code:
$config['survey'][1] = "Question one here...";  // 1-based index, rest can be left empty
$config['survey'][]  = "Question two here...";
... etc.

You could have a Survey controller and a question() method, which takes a single parameter: $number.

Code:
class Survey
{

    function question($number = 1)
    {
        $this->load->config('survey_questions', TRUE);  // load into group
        $questions = $this->config->item('survey');
        if (isset($questions[$number]))
        {
            $data['question_number'] = $number;
            $data['question'] = $question[$number];
            $this->load->view('question_template', $data);
        }
        else
        {
            // invalid question number!
        }
    }

}

El Forum

[eluser]coolfactor[/eluser]
Now, collecting the answers... have an answer() method that collects the answer, and then redirects to the next question. If you don't like redirects, then you could just build the answer functionality into the question() method.

Because we're passing the $question_number into the view, we can use that as a parameter to the answer() method:

Code:
<form method="post" action="/survey/answer/<?php echo $question_number; ?>/">
    <h3>&lt;?php echo htmlentities($question); ?&gt;</h3>
    &lt;input type="submit" name="answer" value="No" /&gt;
    &lt;input type="submit" name="answer" value="Yes" /&gt;
&lt;/form&gt;

Notice how there are 2 submit buttons. Only the one that is clicked is sent back with the form data, so they can have the same name.

I'm sure you can take it from there.

El Forum

[eluser]tinawina[/eluser]
I like this -- and never would have thunk it up on my own. Thanks!

We aren't storing the data because this is truly just a tool that walks people through (really short) questions only to land them at a final answer (given that they've answered all the previous questions). Truly I'm just starting to plan this out. I may need to create this using a database in the not so distant future.... Regardless, your code definitely gives me a new way to think about doing this. Thank you!