I have a MySQL column that holds a JSON string of question numbers (0-165) as a key and question answers (score of 1-5) as a value. I need to retrieve all of the MySQL columns, put them into a .csv file with the JSON values in separate columns.
So far, I have a working PHP function that outputs the entirety of my MySQL table to an CSV file using fputcsv(). Here is the code:
function buildcsv(){
$start = $_POST['start'];
$end = $_POST['end'];
$result = mysql_query('SELECT fullname, institution, email, timestamp, responsesJSON
FROM `members` WHERE timestamp BETWEEN "'.$start.'" AND "'.$end.'"');
if (!$result) die('Couldn\'t fetch records');
ob_start();
$fp = fopen('php://output', 'w');
if ($fp && $result) {
while ($row = mysql_fetch_row($result)) {
fputcsv($fp, array_values($row));
}
fclose($fp);
return ob_get_clean();
}else{
$error = "Error. Either there is no data or invalid date.";
return $error;
}
}
And here is the output from the code:
However, I need my CSV output to resemble the following mockup:
Is it possible to do this without changing my database structure? I have seen code on stackoverflow that puts JSON values into separate columns, but how would I integrate it into my existing code which retrieves all other MySQL columns?
Thank you all in advance.