From 22748866b3bdeffe34e955adeedc231516266703 Mon Sep 17 00:00:00 2001 From: Josh de Leeuw Date: Wed, 18 Feb 2015 11:22:43 -0500 Subject: [PATCH] update example php issue #110 --- docs/markdown_docs/features/data.md | 82 +++++++++++------------------ 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/docs/markdown_docs/features/data.md b/docs/markdown_docs/features/data.md index 76948ac2..3520e0f8 100644 --- a/docs/markdown_docs/features/data.md +++ b/docs/markdown_docs/features/data.md @@ -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 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 ``` @@ -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). \ No newline at end of file +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).