$value){ if(!empty($value)){ $sum = $sum + $value; $n = $n + 1; } } $average = "Error. There were not enough cases to compute an average."; if($n > 0){ $average = $sum/$n; $average = round($average,2); } return $average; } /* ------------------------------------------ If the user is logged in, he or she will have a cookie on his or her machine. If no cookie exists, redirect the user to login page. ------------------------------------------ */ if(!isset($_COOKIE['user_id']) OR !isset($_COOKIE['sessionID']) OR !isset($_COOKIE['first_name']) ){ redirect_user(); exit(); } // show errors ini_set('display_errors',1); // Read in user-submitted CGI data $v01 = $_POST['v01']; $v02 = $_POST['v02']; $v03 = $_POST['v03']; $v04 = $_POST['v04']; $v05 = $_POST['v05']; $v06 = $_POST['v06']; // Read in programmer-generated data $pass = $_POST['pass']; // Where to start? if(empty($pass)){ $pass = 1; } /* We store the item content and the variable names in arrays. Doing so allows us to easily access the content-variable_name pairs independently of the order in which the items appear. In fact, we randomize the order of the items later. */ $item_content = array("It helps to turn to this person in times of need.","I usually discuss my problems and concerns with this person.","I find it easy to depend on this person.","I often worry that this person doesn't really care for me.","I'm afraid that this person may abandon me.","I worry that this person won't care about me as much as I care about him or her."); $item_names = array("v01","v02","v03","v04","v05","v06"); // 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 because PHP starts counting array elements at 0 // Include external style sheet print " "; // Pass = 1 if($pass == 1){ // Determine the randomized order of items $item_index = range(0,$max_index); shuffle($item_index); // Pull out user's first name form cookie. $fn = $_COOKIE['first_name']; print "
"; print "
"; print "

Hello, $fn.

Please answer the following questions about your dating or marital partner. Note: If you are not currently in a dating or marital relationship with someone, answer these questions with respect to a former partner or a relationship that you would like to have with someone.

"; // Note: Because we are presenting all the items in one sweep, I do not have // hidden tag subroutines to pass things forward. Everything we need (minus pass) // is being collected here as used input and will naturally be passed forward // when the user presses submit. // Loop through all the items. Present each one with the appropriate variable name. // I will use the $i counter to ensure that each specific response option has a unique // CSS id for the purpose of ensuring that the inline CSS works appropriately. // I will use $j to create artificial item numbers for the user. $i = 0; $j = 0; foreach ($item_index as $key => $value){ $j = $j+1; $this_item_name = $item_names[$item_index[$key]]; // which variable name to use? $this_item_content = $item_content[$item_index[$key]]; // which item to present now? print"
"; // Note. I ran into a problem in getting the table rows to highlight as // a different color onMouseover. I ran out of time to debug this. // My appologies. $i = $i+1; $this_id = "vid" . $i; print ""; $i = $i+1; $this_id = "vid" . $i; print " "; $i = $i+1; $this_id = "vid" . $i; print " "; $i = $i+1; $this_id = "vid" . $i; print " "; print "
$j $this_item_content
Strongly Disagree
Disagree
Agree
Strongly Aisagree


"; } print "
"; // Pass this manually print ""; print "
"; print "
"; } // End pass == 1 if($pass == 2){ /* SQL code to create the table. We need to run this once in phpMyAdmin before the survey is tested/goes live. CREATE TABLE survey3 ( 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, v06 VARCHAR(1) NOT NULL, avoidance VARCHAR(5) NOT NULL, anxiety VARCHAR(5) NOT NULL, core_user_id VARCHAR(60) NOT NULL, submission_date DATETIME NOT NULL, PRIMARY KEY (user_id) ); */ // Compute composite scores $avoidance_scores = array($v01,$v02,$v03); $anxiety_scores = array($v04,$v05,$v06); $avoidance = average_scores($avoidance_scores); $anxiety = average_scores($anxiety_scores); $avoidance = round($avoidance,2); $anxiety = round($anxiety,2); // Set the database access information as constants DEFINE ('DB_USER', 'rcfraley'); DEFINE ('DB_PASSWORD', 'nurse23'); DEFINE ('DB_HOST', 'yourpersonality.netfirmsmysql.com'); DEFINE ('DB_NAME', 'ullman'); // 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() ); $user_id = $_COOKIE['user_id']; $sessionID = $_COOKIE['sessionID']; // Create a string that represents the database querry $q = "INSERT INTO survey3 (v01,v02,v03,v04,v05,v06,avoidance,anxiety,core_user_id,submission_date) VALUES ('$v01','$v02','$v03','$v04','$v05','$v06','$avoidance','$anxiety','$user_id',NOW() )"; // Run the query $r = @mysqli_query ($dbc, $q); // Let's make a new query to indicate that the user has completed the survey $q2= "UPDATE users SET study3_taken='1', study3_lastdate=NOW(), study3_type='repeat' WHERE user_id='$user_id' AND session='$sessionID' LIMIT 1"; // Run the query $r2 = @mysqli_query ($dbc, $q2); if ($r) { // If it ran OK. print "Your data have been saved. Thank you for your participation!

"; print "Your avoidance score was $avoidance.
"; print "Your anxiety score was $anxiety.
"; print "Return to menu"; }else{ print "There was an error in the data base Q1. Please contact the site administrator.
"; } if ($r2) { // If it ran OK. //print "users updated okay "; }else{ print "There was an error in the data base Q2. Please contact the site administrator.
"; } // Close the database connection. mysqli_close($dbc); } // End pass == 2 ?>