0

I'm building a multipage form for an online experiment, which will involve showing different versions of the questions to different users. Is there a simple way to create page sequences that incorporate different combinations of the page files? Say I have these files: 1A.php and 1B.php (page 1); 2A.php and 2B.php (page 2); 3A.php, 3B.php, 3C.php, 3D.php, 3E.php and 3F.php (page 3) and so on, how would I go about creating a set of unique navigation paths? For instance, one might be [1A.php -> 2B.php -> 3E.php -> 4B.php] while another is [1A.php -> 2B.php -> 3A.php -> 4C.php]. (I'm new to PHP so I suspect this might not be the most sophisticated way of doing things, but I'm happy as long as something works.) Each page has an <input type="submit"> button and connects to a database via a separate PHP file.

The idea is to randomly redirect users from the start page to one of the (12) preset sequences.

Any suggestions?

Edit: The objective here is not to generate every possible set of questions. To clarify, the objective is to specify 12 of the possible page combinations (for reasons to do with the experimental design). The questionnaire will have a start page, and from here I'd like to redirect respondents to one of 12 branches.

tui
  • 37
  • 8
  • when you submit at the moment, do all the pages submit to 1 "processing" file and then redirect to the next page? or have you not got that far yet? – CᴴᵁᴮᴮʸNᴵᴺᴶᴬ Feb 27 '13 at 09:00
  • @DannyHearnah The "processing" file also contains instructions for redirection but that was before the page variations were added. In any case, I have the option of simply sticking with what I've got, and uploading the 12 conditions to separate directories (meaning lots of duplicates). – tui Feb 27 '13 at 10:16
  • i have a solution for you, i will post the answer now – CᴴᵁᴮᴮʸNᴵᴺᴶᴬ Feb 27 '13 at 11:18

4 Answers4

0

So you want to load diferent content into the page based on your choice? if yes do something like this :

<a href="1A.php?id=1B">1B</a>
if(isset($_GET["id"])){

//loads page 1A.php?id=1B with whatever html or php you want to load

}
else {

//loads page 1A.php ,with default content

}

You can add any number of links you want,and load all you content in 1 single page, albeit you have to write the code for each page in one gigantic switch if you have more than two pages OR load the content from the database (which i recommend).

Johny
  • 163
  • 1
  • 11
0
$questionaires = array('1a','1b',...,'2a','2b',...,'3a'...);//list all those filenames here

function generate_questionaire(&$array)
{
    $key = rand(0, count($array);
    unset($array[$key]);
    return $key;
}

so call that function every time you want to include a `php` file thus:
<?php
$questionNum = generate_questionaire($questionauires);
include_once($questionNum.'.php');
?>
ianace
  • 1,646
  • 2
  • 17
  • 31
0

This should do the trick: http://pyrus.sourceforge.net/Math_Combinatorics.html from the answer https://stackoverflow.com/a/10466745/1481489 You're talking about permutations by the way: Permutations - all possible sets of numbers

Community
  • 1
  • 1
zamnuts
  • 9,492
  • 3
  • 39
  • 46
0

In your form, on each page, have a hidden field called step like so

<input type="hidden" name="step" value="1"> // this is for step 1.. value="2" for step 2 etc

In your processing file before the redirect do the following

$nextstep = (int)$_POST['step'];
$nextstep++;
$optionsarray = array('A','B','C','D','E','F','G','H','I','J','K','L');
header('Location: ' . $nextstep . $optionsarray[rand(0,11)] . '.php');

This will randomly select one of the options.. but assign the next step number (1,2,3 etc)

I think this is what you are after..

Edit: what about this

$nextstep = (int)$_POST['step'];
$nextstep++;
$stepsarray = array('1'=>5,'2'=>6,'3'=>2,'4'=>6); // '1'=>5 (step 1, 5 posibilities)  '2'=>6 (step 2, 6 posibilities) etc..
$optionsarray = array('A','B','C','D','E','F','G','H','I','J','K','L');
header('Location: ' . $nextstep . $optionsarray[rand(0,$stepsarray[$nextstep])] . '.php');

So what this does is have an array called $stepsarray - this tells you how many possibilities for each step.. and then the url is generated using rand still, but max number possible is the next steps max possibility.. that may work better for you but if you are happy with how you have set it up then don't worry :-)

  • Thanks - this suggestion is a little different to what I intended but is fine for the task. What I ended up doing is creating a redirect file for each question and calling it from each page like this ``. This page randomly assigns the header based on your code `$optionsarray = array('A','B','C','D','E','F'); header('Location: ' . '4' . $optionsarray[rand(0,5)] . '.php');` since the number of conditions varies between questions. I've kept the original code from the "processing" file that redirects to `nextURL`. – tui Feb 28 '13 at 06:06
  • @tui im glad it helped - i have made an edit which may help you more to keep it in 1 processing file – CᴴᵁᴮᴮʸNᴵᴺᴶᴬ Feb 28 '13 at 09:46