Hi,

I am very new to php but I have been using it to do small web-based experiments (surveys) that have several alternative versions. I need to get an equal number of respondents for each version of the survey. So far what I've been using is a randomization variable for the indexes of the different surveys, see below. This gives me an approximate even amount, and then I manual go and remove survey versions that have enough participants until I have approx the same number of respondents for each version.

There must be a better way of doing this, I'm sure. I would just like to be able to say "Get 10 of each of these four versions" and let the thing run...but that would require keeping track of which version have been responded to. Right now each response gets saved in a file with the number of the version, so theoretically I could somehow look there to check, and perhaps have something like if X > 9, do survey Y...but I have no idea how to do this, and actually, I have no idea even what search terms I might look with to find help online somewhere.

Below is an example of what I'm now using:

<form name="info" method="post" action="<?php
$number = rand(1,4);
if($number == 1){
echo 'http://host/Task/survey.1.php'; }
elseif($number == 2){
echo 'http://host/Task/survey.2.php'; }
elseif($number == 3){
echo 'http://host/Task/survey.3.php'; }
elseif($number == 4){
echo 'http://host/Task/survey.4.php'; }
?>">

Thanks for any help, I'm really quite frustrated!

/JennyK

Ok i have found the solution :) , here it is:

You make a file named "number.txt" in which you store the number. In PHP you retrieve the number and then +1's it if it isn't 4, else you set the number to 1. Then you save the new number into number.txt.

The code:

number.txt:

1

The form:

<form name="info" method="post" action="
<?php
//
// Opens and reads the file number.txt
//
$file="number.txt";
$filehandle = fopen($file, "r");
$number = fread($filehandle, filesize($file));
fclose($filehandle);
//
// Decides which number $nextnumber should be
//
if ($number < 4) {
$nextnumber = $number + 1;
} else {
$nextnumber = 1;
}
//
// Decides which survey will be showed
//
if($number == 1){
echo 'http://host/Task/survey.1.php'; }
elseif($number == 2){
echo 'http://host/Task/survey.2.php'; }
elseif($number == 3){
echo 'http://host/Task/survey.3.php'; }
elseif($number == 4){
echo 'http://host/Task/survey.4.php'; }
//
// Writes the new number into the file
//
$filehandle2 = fopen($file, "w");
fwrite($filehandle2, $nextnumber);
fclose($filehandle2);
?>
">

Greets, Graphix

Member Avatar for diafol

Thanks for any help, I'm really quite frustrated!

Woaah, slow down Tiger.

Just ONE way to do this would be:

surveys table
id (autoincrement/tinyint/primary key)
survey_name (varchar)
max_respondants (int - stop collecting after certain amount)
active (tinyint - can switch off the survey manually)
form_html (text - place the 'cleaned' form html in here - just the inside 'guts' of the form - no form tags)

replies table
id (autoincrement/tinyint/primary key)
survey_id (foreign key on id in surveys table)
replies (text) - I'd use php functions serialize() and unserialize() to store the form $_POST variables as an array


Now this is a very basic setup, and somewhat complicates collating the data from the replies field due to the 'array' nature, but it's usable. I'm not using the active and max_respondants fields here, just for clarity.

At the top of your page, enter the code for finding the survey with the lowest number of respondants, something like:

<?php
$rs = mysql_query("SELECT surveys.id,surveys.form_html, COUNT(replies.survey_id) AS count1 FROM replies INNER JOIN surveys ON replies.survey_id = surveys.id GROUP BY replies.survey_id ORDER BY count1");
//get first record 
$d = mysql_fetch_array($query);

//the "uncleaned" form html to display
$form_html = stripslashes(html_entity_decode($d['form_html']));
//the id of the form so the db knows which survey id to apply to the saved data 
$id = $d['survey_id'];
?>
<form id=".." name="..." action="formhandler.php?id=" method="post">
<?php echo "<input type ='hidden' name='survey_id' id='survey_id' value='{$id}' />" . $form_html;?>
</form>

All your forms will be sent to the formhandler.php page, where all you do is send the data to the db (after 'cleaning' it).
For 'replies' table:
The $_POST field will give the survey_id field
Next delete the $_POST so it doesn't interfere with your $_POST array:

unset($_POST['survey_ id']);

The serialize($_POST) function should work to put the replies into a single field.

//EDIT//
Sorry G - went for a metaphysical walk during my reply.

Dear Graphix and Ardav,

Thanks so much! This is great! I was able to immediately understand your solution, Graphix, and I've gotten it to work! This really solves a big problem for me.

I haven't fully understood your solution yet Ardav, but I can see that this would give me much more control over the surveys and really simplify things. I need some time to study it though.

Thanks so much to both of you!
/JennyK

Member Avatar for diafol

Dear Graphix and Ardav,

Thanks so much! This is great! I was able to immediately understand your solution, Graphix, and I've gotten it to work! This really solves a big problem for me.

I haven't fully understood your solution yet Ardav, but I can see that this would give me much more control over the surveys and really simplify things. I need some time to study it though.

Thanks so much to both of you!
/JennyK

No worries Jen, G's reply is easy to implement if you just want a quick fix w.r.t. showing different forms and I suggest that you use that.

If this thread is still alive, could you mention how you're collating the data? Do you get one form and manually add data to a spreadsheet for example, or are your data items stored in a db? When feeling up to a challenge, you could get create some code to automatically collate your data for you and even create graphs for certain response types.

Dear Ardav,

Well, I have a fairly primitive collection method for the data: have an array storing answers from questions posed at the index file (same for all participants), and then another array with the answers to questions posed at the survey file (with several different versions).

function setup_file(){
               global $id;
       ignore_user_abort(true);
       if(!file_exists("participants/$id.cht3_test1.results")){
               $handle = fopen("participants/$id.cht3_test1.results","a");
     //  foreach($_POST as $key => $value){
           //           fputs($handle,"% ".$key.": ".$value."\n");
             //         }
               }
       ignore_user_abort(false);
          }

function process_results(){
                     global $id;
         global $_POST;
         ignore_user_abort(true);
         $handle = fopen("participants/$id.cht3_test1.results","a");
         fputs($handle,"* ".gmdate("F j, Y, H:i:s")."\n");
         foreach($_POST as $key => $value){
                          if($key{0}=="Q"){
                             fputs($handle,$key.": ".$value."\n");
                             }
                          }
         fclose($handle);
         ignore_user_abort(false);           
                     }

I have to do a number of transformations of each of these files, and of course, and put them all in one file, to get the file format I need for the analysis (in SPSS).

I actually didn't write the original php, so I didn't actually write the above, I just know when to use it...so I haven't attempted anything more sophisticated because it is so hard to predict how long it will take (me) to be successful.

But I suppose I just have to create a file, open it for writing each time a survey is called, and then somehow append the values of the array separated by comma's on the same line (how do you avoid getting a new line character after each value), and then close the file....hmmm Now when I write it out it might not be that hard...

Any suggestions? This is maybe not the best way to do this, but it does seem comprehensible to me.

Thanks for your further interest and any help/comments.

/JennyK

Member Avatar for diafol
fputs($handle,$key.": ".$value."\n");

Change the "\n" to a ",". However, you may need to input a "\n" at the start of the entry before the loop.

That's the offending bit that forced a new line after each value. Ideally you'd want to create a csv file for something like SPSS (I assume) - I never had much to do with SPSS, I tended to use Minitab, so I may be wrong here.

I don't know if SPSS could extract data from a db anyway - perhaps you'd have to create an xml file. In which case, you'd possibly be better off just sticking to your current setup (@ officionados - don't shoot me!).

You may find that php/MySQL could replicate the statistical calculations of SPSS. There's a PECL library and plenty of stats scripts online. I've seen some for ANOVAs, multiple range tests (e.g. Tukey), t-tests, calc of s.d., s.e.m, variance etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.