| | |
Control number of survey participants in php?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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:
The form:
Greets, Graphix
, 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
The form:
php Syntax (Toggle Plain Text)
<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
Last edited by Graphix; Sep 22nd, 2009 at 12:37 pm.
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:
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:
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.
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)
<?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']; ?>
PHP Syntax (Toggle Plain Text)
<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['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)
unset($_POST['survey_ id']);
//EDIT//
Sorry G - went for a metaphysical walk during my reply.
Last edited by ardav; Sep 22nd, 2009 at 1:26 pm. Reason: uurghhhhhhhh!
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
•
•
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
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.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
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
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
PHP Syntax (Toggle Plain Text)
fputs($handle,$key.": ".$value."\n");
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.
If you don't reply to your own thread or you can't find the solved link - you're off my Christmas list - permanently! Bah humbug!
![]() |
Similar Threads
- How do I pass values with a link into mySQL database using PHP? (PHP)
- Php help please. Web survey (PHP)
- Can I control the number of IE windows that open? (Web Browsers)
- Newb PHP question (PHP)
- Moving avatars (C++)
- hello, and a request to participate in a survey! (Geeks' Lounge)
- PHP - Half the way response and continue PDF processing (PHP)
- Control number of IE windows that open. (Web Browsers)
Other Threads in the PHP Forum
- Previous Thread: PHP Confused!
- Next Thread: count checkboxes
Views: 895 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for php, survey
.net ajax apple array asp beginner cellphone certification checkbox class cms community connectivity crime curl data database date display email encryption enter execute explodefunction file files flash flex form forms function functions gaming gartner google html image include integration internet iphone java javascript jquery lamp limit link linux login mail media menu methods microsoft mobile multiple mysql navigation networks news oop panic password paypal php post provider query research results script search security session sex sms soap social software sorting spam spyware sql structure survey tutorial twitter uk upload validation vbulletin video virtual web webdesign window xml xslt youtube zend







