\n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; print " \n"; // hidden ARRAYS // Note: We have to use isset to see if the array exists otherwise the foreach loop // will generate an error. if(isset($_POST['item_index']) | $isset){ foreach ($_POST['item_index'] as $key => $value){ echo ' '; print " \n"; } } } /* Here are some arrays we could, theoretically, want access to at any stage of the game. Thus, let's initialize them here and do so fresh on each pass rather than passing them forward as hidden tags. (In general, if there is no need to pass data via hidden tags, then don't do it.) */ $item_content = array("tired","hungry","anxious","sad","excited"); // The content/items to be presented $item_names = array("v01","v02","v03","v04","v05"); // The variable names that will be associated with each item $number_of_items = count($item_names); // How many items/trials are there? $max_index = $number_of_items - 1; // Let's take 1 minus the above /* If pass == 1, then that signals to us that the user should receive whatever information we want to display on the FIRST VISIT (e.g., consent information, overview of study). */ // Pass = 1 if($pass == 1){ /* In addition to presenting a greeting on the first page(pass), we are also going to decide at this moment the random order in which the stimuli/items will be presented. We will do so by - creating an array that contains an index for each item (i.e., its place in the array file drawer). - shuffling/randomizing the order of that array - using that shuffled array to index the actual items (and their NAMEs) - Finally, we preserve that random order by passing it forward as hidden data */ $item_index = range(0,$max_index); shuffle($item_index); print "

Hello! Welcome to my website.

Please tell me your name.

"; /* Create HIDDEN tags for ALL information. Do this at start of FORM. */ passhidden(); /* ------------------------------------------------------------------------------------ Note. The first time we create item_index, we WILL need to pass it manually as a HIDDEN tag. Note: We do the same with pass below. ------------------------------------------------------------------------------------ */ foreach ($item_index as $key => $value){ echo ' '; print " \n"; } print "

name

"; print ""; // Pass this manually the first time print ""; print "
"; } // End pass == 1 /* If pass>= 1 AND <= 7, then let's present the trials or items. Each time through, we increment pass by a value of 1. Thus, we end the "loop" once pass is 8 or higher. */ // We're going to skip the instructions this time, so setting $pass <= 6 instead of <= 7 if($pass >= 2 AND $pass <= 6){ print "
"; /* Create HIDDEN tags for ALL information. Do this at start of FORM. */ passhidden(); print "Do you currently feel...
"; $pass_index = $pass - 2; // let's count from 0 as we move through this section $this_item_content = $item_content[$item_index[$pass_index]]; // which item to present now? $this_item_name = $item_names[$item_index[$pass_index]]; // which item name to use? print "

$this_item_content

"; print ""; /* Here we are using a new trick. Specifically, instead of manually setting pass to the appropriate value (e.g., $pass=4;), we are going to increment it by 1. Thus, as the person moves from one item to the next, we increment pass by 1. This allows us to ensure that the person is getting the appropriate item at each step along the way. Notice also that, instead of writing $pass = $pass + 1, I wrote $nextpass = $pass + 1. I did this to ensure that I was not overwriting the current value of pass within the script. There are no ill consequenes of doing so here, but there are other situations in which it could matter. Thus, this is a generic solution to a problem that doesn't exist and, now, will never exist. */ $nextpass = $pass + 1; print ""; // Pass this manually because we've modified it print "
"; } // End if($pass >= 2 OR $pass <= 6) if($pass == 7){ /* SQL code to create the table. We need to run this once in phpMyAdmin CREATE TABLE demo1 ( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, v01 VARCHAR(1) NOT NULL, v02 VARCHAR(1) NOT NULL, v03 VARCHAR(1) NOT NULL, v04 VARCHAR(1) NOT NULL, v05 VARCHAR(1) NOT NULL, myname VARCHAR(60) NOT NULL, submission_daate DATETIME NOT NULL, PRIMARY KEY (user_id) ); Open a MySQL connection */ // Set the database access information as constants /* The reason we do this in a two-step process is (a) It makes it easier for us to see, at a glance, what the relevant parameters are. Thus we can update things via copy-and-paste from one application to the next with little effort. (b) It is slightly more secure this way. */ DEFINE ('DB_USER', 'rcfraley'); DEFINE ('DB_PASSWORD', '*password*'); DEFINE ('DB_HOST', 'yourpersonality.netfirmsmysql.com'); DEFINE ('DB_NAME', 'class'); // Open the database connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); // Create a string that represents the database querry $q = "INSERT INTO demo1 (v01,v02,v03,v04,v05,myname,submission_daate) VALUES ('$v01','$v02','$v03','$v04','$v05','$myname',NOW() )"; // Run the query $r = @mysqli_query ($dbc, $q); /* If the query ran with no errors, what is returned ($r) will 'exist' Thus, we can make our next steps (e.g., saying thank you) contingent upon a successful return of $r. If we are requesting information from the databse (vs. INSERTing information in it), $r will hold those values and we can sort through them using loops. */ if ($r) { // If it ran OK. print "Your data have been saved. Thank you for your participation!
"; }else{ print "There was an error in the data base. Please try again.
"; } /* Let's extract information from the data base. We'll create a SELECT querry to pull out the data for v01, v02 ... Then, within a while loop and using mysqli_fetch_array($r, MYSQLI_BOTH), we will cycle through each row of the database. The selected data for that person/row will be represented as an array that we will reference as $row. We can reference the information within that array either by using the variable name $row['v01'] or via the index value $row[0]. */ // Create a string that represents the database querry $q = "SELECT v01,v02,v03,v04,v05 FROM demo1"; // Run the query $r = @mysqli_query ($dbc, $q); if($r){ // We'll do some counting. So let's start by setting some values to zero. $sumv01 = 0; $sumv02 = 0; $n = 0; // Loop through each row that was returned from the database query. // As we loop through each line of data, we'll add the value of v01 // to a running total. We'll do the same with v02. We will count the number // of cases as $n. while($row = mysqli_fetch_array($r, MYSQLI_BOTH)){ $sumv01 = $sumv01 + $row['v01']; $sumv02 = $sumv02 + $row['v02']; $n = $n + 1; } // Let's do something useful with this information. Such as returning it // as feedback. print "How many people in our sample are tired? $sumv01 of $n
"; print "How many people in our sample are hungry? $sumv02 of $n
"; }else{ print "There was an error in accessing the data base part 2. Please try again.
"; } // Close the database connection. mysqli_close($dbc); } // End pass == 7 ?>