$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; } // Show PHP errors in browser ini_set('display_errors',1); // 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(); } $user_id = $_COOKIE['user_id']; $tempID = $_COOKIE['sessionID']; $first_name = $_COOKIE['first_name']; // If cookie exists, continue // Include external style sheet print " "; print "
"; print ""; print ""; print ""; // Set the database access information as constants: DEFINE ('DB_USER', 'rcfraley'); DEFINE ('DB_PASSWORD', '*PASSWORD*'); DEFINE ('DB_HOST', 'yourpersonality.netfirmsmysql.com'); DEFINE ('DB_NAME', 'ullman'); // Make the database connection: $dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() ); // Define a SQL query // We will select from the data table (survey3) the user's composite scores. // We only select these data from rows in which // the core_user_id == the user_id in the cookie. $q = "SELECT avoidance,anxiety,submission_date FROM survey3 WHERE core_user_id='$user_id'"; $r = @mysqli_query ($dbc, $q); // Run the query. print ""; print ""; print "
"; print "Welcome, $first_name!

"; print "Here is an overview and summary of your scores.

"; print "
"; while($row = mysqli_fetch_array($r, MYSQLI_BOTH) ) { print "On $row[2] :
"; print "Your avoidance score was $row[0]
"; print "Your anxiety score was $row[1]

"; } // Select ALL avoidance, anxiety scores, regardless of who submitted them. $q2 = "SELECT avoidance,anxiety FROM survey3"; $r2 = @mysqli_query ($dbc, $q2); // Run the query. // Initialize some arrays to hold sample scores $sample_avoidance = array(); $sample_anxiety = array(); // Add each composite score to the array as we loop through the data while($row = mysqli_fetch_array($r2, MYSQLI_BOTH) ) { $sample_avoidance[] = $row[0]; $sample_anxiety[] = $row[1]; } // Compute sample means $sample_mean_avoidance = average_scores($sample_avoidance); $sample_mean_anxiety = average_scores($sample_anxiety); print "
"; print "The sample mean for avoidance is $sample_mean_avoidance.
"; print "The sample mean for anxiety is $sample_mean_anxiety.
"; print "
"; print "

Return to menu "; print "
"; // Close the database connection. mysqli_close($dbc); ?>