update example php

issue #110
This commit is contained in:
Josh de Leeuw 2015-02-18 11:22:43 -05:00
parent 37c7966d9c
commit 22748866b3

View File

@ -56,23 +56,15 @@ There are dozens of database options. MySQL is one of the most popular [relation
### 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
// Josh de Leeuw
// 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
// Edit this line to include your database connection script
//
// The script you link should contain the following two lines:
//
@ -94,24 +86,12 @@ function mysql_insert($table, $inserts) {
$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