mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-11 16:18:11 +00:00
parent
37c7966d9c
commit
22748866b3
@ -1,6 +1,6 @@
|
||||
# Data Storage
|
||||
|
||||
There are two very different kinds of data storage: data stored in *memory* and data stored *permanently*. Data stored permanently exists even after the browser running jsPsych closes, typically in a database or in a file on a web server. Data stored in memory exists only as long the browser window running jsPsych is open.
|
||||
There are two very different kinds of data storage: data stored in *memory* and data stored *permanently*. Data stored permanently exists even after the browser running jsPsych closes, typically in a database or in a file on a web server. Data stored in memory exists only as long the browser window running jsPsych is open.
|
||||
|
||||
jsPsych has many features for interacting with data stored in *memory*, but relatively few for *permanent* data storage. This is a deliberate choice, mainly because there are dozens of ways that data could be stored permanently and this strategy avoids locking in one particular solution. However, saving data permanently is obviously a crucial component of any experiment, and this page contains a few suggestions on how to accomplish permanent data storage.
|
||||
|
||||
@ -39,9 +39,9 @@ function saveData(filename, filedata){
|
||||
|
||||
// call the saveData function after the experiment is over
|
||||
jsPsych.init({
|
||||
|
||||
|
||||
// code to define the experiment structure would go here...
|
||||
|
||||
|
||||
on_finish: function(data){ saveData("filename.csv", jsPsych.data.dataAsCSV()) }
|
||||
});
|
||||
```
|
||||
@ -50,29 +50,21 @@ To use this in an actual experiment, it would be important to tie the filename t
|
||||
|
||||
## Storing data permanently in a MySQL database
|
||||
|
||||
The ideal solution for storing data generated by jsPsych is to write it to a database.
|
||||
The ideal solution for storing data generated by jsPsych is to write it to a database.
|
||||
|
||||
There are dozens of database options. MySQL is one of the most popular [relational databases](http://en.wikipedia.org/wiki/Relational_database), is free to use, and relatively easy [to install](https://www.google.com/search?q=how+to+install+mysql). This page will assume that you have a MySQL database installed on your server that is hosting the jsPsych experiment, and that your server is able to execute PHP code.
|
||||
|
||||
### Step 1
|
||||
|
||||
To communicate with a MySQL database, you will need a server-side script, such as a PHP script. The following script should work for all jsPsych data. Copy the code below into a PHP file and give it an appropriate name (e.g. savedata.php). Put the PHP file on your server in a convenient location.
|
||||
To communicate with a MySQL database, you will need a server-side script, such as a PHP script. The following script should work for all jsPsych data. Copy the code below into a PHP file and give it an appropriate name (e.g. savedata.php). Put the PHP file on your server in a convenient location. **This script will only work with jsPsych version 4.0 and later.**
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
|
||||
// Submit Data to mySQL database
|
||||
// Josh de Leeuw, August 2012
|
||||
|
||||
// Usage:
|
||||
// You need to POST the following variables to the script (e.g. using jQuery.ajax)
|
||||
// "table": name of the mysql table to insert data into
|
||||
// "json": json-encoded object that contains the data
|
||||
// "opt_data": an associative array of additional variables to write into each row (e.g. subject ID)
|
||||
//
|
||||
//
|
||||
//
|
||||
// EDIT THE FIRST LINE OF CODE BELOW TO CONNECT TO YOUR DATABASE
|
||||
// Josh de Leeuw
|
||||
|
||||
// Edit this line to include your database connection script
|
||||
//
|
||||
// The script you link should contain the following two lines:
|
||||
//
|
||||
@ -80,58 +72,46 @@ To communicate with a MySQL database, you will need a server-side script, such a
|
||||
// mysql_select_db('databasename', $dbc);
|
||||
//
|
||||
include('database_connect.php');
|
||||
|
||||
|
||||
// You should not need to edit below this line
|
||||
|
||||
|
||||
function mysql_insert($table, $inserts) {
|
||||
$values = array_map('mysql_real_escape_string', array_values($inserts));
|
||||
$keys = array_keys($inserts);
|
||||
|
||||
|
||||
return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
|
||||
}
|
||||
|
||||
// get the table name
|
||||
|
||||
// get the table name
|
||||
$tab = $_POST['table'];
|
||||
|
||||
|
||||
// decode the data object from json
|
||||
$trials_obj = json_decode($_POST['json']);
|
||||
|
||||
$trials = json_decode($_POST['json']);
|
||||
|
||||
// get the optional data (decode as array)
|
||||
$opt_data = json_decode($_POST['opt_data'], true);
|
||||
$opt_data_names = array_keys($opt_data);
|
||||
|
||||
// go through each element of the trials object to see if there is data
|
||||
// (some plugins may not have data associated with them)
|
||||
$trials = $trials_obj[0];
|
||||
for($i=1; $i<count($trials_obj); $i++)
|
||||
{
|
||||
if(count($trials_obj[$i])>0){
|
||||
// if there is data, merge that data into the $trials array
|
||||
$trials = array_merge($trials, $trials_obj[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
// this outputs the trials array as a string. Useful for debugging in the JavaScript console.
|
||||
|
||||
var_dump($trials);
|
||||
|
||||
|
||||
// for each element in the trials array, insert the row into the mysql table
|
||||
for($i=0;$i<count($trials);$i++)
|
||||
{
|
||||
$to_insert = (array)($trials[$i]);
|
||||
// add any optional, static parameters that got passed in (like subject id or condition)
|
||||
for($j=0;$j<count($opt_data_names);$j++){
|
||||
$to_insert[$opt_data_names[$j]] = $opt_data[$opt_data_names[$j]];
|
||||
}
|
||||
$result = mysql_insert($tab, $to_insert);
|
||||
$to_insert = (array)($trials[$i]);
|
||||
// add any optional, static parameters that got passed in (like subject id or condition)
|
||||
for($j=0;$j<count($opt_data_names);$j++){
|
||||
$to_insert[$opt_data_names[$j]] = $opt_data[$opt_data_names[$j]];
|
||||
}
|
||||
$result = mysql_insert($tab, $to_insert);
|
||||
}
|
||||
|
||||
|
||||
// confirm the results
|
||||
if (!$result) {
|
||||
die('Invalid query: ' . mysql_error());
|
||||
die('Invalid query: ' . mysql_error());
|
||||
} else {
|
||||
print "successful insert!";
|
||||
print "successful insert!";
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
```
|
||||
|
||||
@ -160,7 +140,7 @@ mysql_select_db('myresearch', $dbc);
|
||||
```
|
||||
|
||||
### Step 3
|
||||
|
||||
|
||||
|
||||
To use this PHP script, you need to invoke it from JavaScript code within your experiment page. Here's an example of how to do that.
|
||||
|
||||
@ -187,4 +167,4 @@ function save_data(data){
|
||||
}
|
||||
```
|
||||
|
||||
Note that you'll need to change the script above to reference the table in your mysql database that will store the data, the path to the PHP file created in step 1, and change the opt_data line to include any data you want to append to the table, such as a subject ID (or remove this line entirely if you have no additional data).
|
||||
Note that you'll need to change the script above to reference the table in your mysql database that will store the data, the path to the PHP file created in step 1, and change the opt_data line to include any data you want to append to the table, such as a subject ID (or remove this line entirely if you have no additional data).
|
||||
|
Loading…
Reference in New Issue
Block a user