943,844 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1790
  • PHP RSS
Sep 22nd, 2009
0

Control number of survey participants in php?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JennyK is offline Offline
6 posts
since Oct 2008
Sep 22nd, 2009
0

Re: Control number of survey participants in php?

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:
PHP Syntax (Toggle Plain Text)
  1. 1

The form:
php Syntax (Toggle Plain Text)
  1. <form name="info" method="post" action="
  2. <?php
  3. //
  4. // Opens and reads the file number.txt
  5. //
  6. $file="number.txt";
  7. $filehandle = fopen($file, "r");
  8. $number = fread($filehandle, filesize($file));
  9. fclose($filehandle);
  10. //
  11. // Decides which number $nextnumber should be
  12. //
  13. if ($number < 4) {
  14. $nextnumber = $number + 1;
  15. } else {
  16. $nextnumber = 1;
  17. }
  18. //
  19. // Decides which survey will be showed
  20. //
  21. if($number == 1){
  22. echo 'http://host/Task/survey.1.php'; }
  23. elseif($number == 2){
  24. echo 'http://host/Task/survey.2.php'; }
  25. elseif($number == 3){
  26. echo 'http://host/Task/survey.3.php'; }
  27. elseif($number == 4){
  28. echo 'http://host/Task/survey.4.php'; }
  29. //
  30. // Writes the new number into the file
  31. //
  32. $filehandle2 = fopen($file, "w");
  33. fwrite($filehandle2, $nextnumber);
  34. fclose($filehandle2);
  35. ?>
  36. ">

Greets, Graphix
Last edited by Graphix; Sep 22nd, 2009 at 12:37 pm.
Reputation Points: 82
Solved Threads: 74
Posting Pro in Training
Graphix is offline Offline
401 posts
since Aug 2009
Sep 22nd, 2009
-1

Re: Control number of survey participants in php?

Click to Expand / Collapse  Quote originally posted by JennyK ...
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $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");
  3. //get first record
  4. $d = mysql_fetch_array($query);
  5.  
  6. //the "uncleaned" form html to display
  7. $form_html = stripslashes(html_entity_decode($d['form_html']));
  8. //the id of the form so the db knows which survey id to apply to the saved data
  9. $id = $d['survey_id'];
  10. ?>
PHP Syntax (Toggle Plain Text)
  1. <form id=".." name="..." action="formhandler.php?id=" method="post">
  2. <?php echo "<input type ='hidden' name='survey_id' id='survey_id' value='{$id}' />" . $form_html;?>
  3. </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['survey_id'] field will give the survey_id field
Next delete the $_POST['survey_id'] so it doesn't interfere with your $_POST array:
PHP Syntax (Toggle Plain Text)
  1. 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.
Last edited by ardav; Sep 22nd, 2009 at 1:26 pm. Reason: uurghhhhhhhh!
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,692 posts
since Oct 2006
Sep 23rd, 2009
0

Re: Control number of survey participants in php?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JennyK is offline Offline
6 posts
since Oct 2008
Sep 23rd, 2009
-1

Re: Control number of survey participants in php?

Click to Expand / Collapse  Quote originally posted by JennyK ...
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.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,692 posts
since Oct 2006
Sep 25th, 2009
0

Re: Control number of survey participants in php?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JennyK is offline Offline
6 posts
since Oct 2008
Sep 25th, 2009
-1

Re: Control number of survey participants in php?

PHP Syntax (Toggle Plain Text)
  1. 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.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 948
Sarcastic Poster
ardav is offline Offline
6,692 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP Confused!
Next Thread in PHP Forum Timeline: count checkboxes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC