", print_r($_POST, true), "
"; // show errors ini_set('display_errors',1); /* Read in CGI Data Let's begin by reading in all the data that are relevant to our project. Note: The simplist way to do this is to read in, quite literally, EVERYTHING that we will need at ANY point during the project--whether it exists at this pass or not. */ $v01 = $_POST['v01']; $pass = $_POST['pass']; $this_condition = $_POST['this_condition']; // Where to start? // If pass is empty, assume it is the person's first time here // and set pass to 1 to start. if(empty($pass)){ $pass = 1; } /* We're going to assign people to one of four conditions in a 2x2 bewteen-persons experiement. In this example, I haven't literally varied two factors. But the basic process is the same. In a 2x2 between subjects experiment, we would have 4 conditions and the probability of being assigned to any one of those 4 is 1/4. */ $condition = array("condition 1","condition 2","condition 3","condition 4"); // Pass = 1 if($pass == 1){ // print_r($condition); // Let's shuffle the condition array shuffle($condition); // print_r($condition); // And then select the 0th element of it as the condition for this participant $this_condition = $condition[0]; print "\$this_condition = $this_condition
"; /* In this example experiment, we will display one of four unique images, depending on what condition the user has been assigned. Specifically, we will show 1 of 4 lolcats images and ask for a rating of the image. To do this, we simply create a variable, $this_stimulus, that will represent the name of the image file that we wish to display. We set this variable via if-conditions. */ print "
"; /* Create HIDDEN tags for ALL information. Do this at start of FORM. */ print "\n"; print "\n"; print "\n"; if($this_condition == "condition 1"){ $this_stimulus = "lolcat1.jpg"; } if($this_condition == "condition 2"){ $this_stimulus = "lolcat2.jpg"; } if($this_condition == "condition 3"){ $this_stimulus = "lolcat3.jpg"; } if($this_condition == "condition 4"){ $this_stimulus = "lolcat4.jpg"; } print "
"; print "
"; print "How much do you like this photo?
"; print "Not at all "; print " "; print " "; print ""; print ""; print ""; print "Very much

"; print ""; print ""; print "
"; print ""; print "
"; } // End pass == 1 // If we have the rating, we're done. Say thank you. // Let's also save the data if($pass >= 2 ){ print "\$this_condition = $this_condition
"; $date = getdate(); $date1 = $date[mon] . "/" . $date[mday] . "/" . $date[year]; $date2 = $date[hours] . ":" . $date[minutes] . ":" . $date[seconds]; // Let's create a variable that contains--as one long text string--all the information // we wish to save for the user. $stringData = "\n" . $date1 . "," . $date2 . "," . $this_condition . "," . $v01 . "," . "endline"; // Finally open the file, place the data within, then close the file. $myFile = "PHPtutorial12Data.txt"; // set the datafile name $fh = fopen($myFile, 'a') or die("can't open file"); // open the file a = append fwrite($fh, $stringData); // place the data in the file fclose($fh); // close the file print "Thank you for your rating!"; } ?>