if ($spssver == 2) { echo ""; } $na = ""; spss_export_data($na); exit; } if ($subaction == 'dlstructure') { header("Content-Disposition: attachment; filename=survey_" . $surveyid . "_SPSS_syntax_file.sps"); header("Content-type: application/download; charset=UTF-8"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Pragma: public"); // Build array that has to be returned $fields = spss_fieldmap(); //Now get the query string with all fields to export $query = spss_getquery(); $result = db_execute_num($query) or safe_die("Couldn't get results<br />{$query}<br />" . $connect->ErrorMsg()); //Checked $num_fields = $result->FieldCount(); //Now we check if we need to adjust the size of the field or the type of the field while ($row = $result->FetchRow()) { $fieldno = 0; while ($fieldno < $num_fields) { //Performance improvement, don't recheck fields that have valuelabels if (!isset($fields[$fieldno]['answers'])) { $strTmp = mb_substr(strip_tags_full($row[$fieldno]), 0, $length_data); $len = mb_strlen($strTmp); if ($len > $fields[$fieldno]['size']) { $fields[$fieldno]['size'] = $len; } if (trim($strTmp) != '') {
function spss_export_data($na = null) { global $length_data; // Build array that has to be returned $fields = spss_fieldmap(); //Now get the query string with all fields to export $query = spss_getquery(); $result = db_execute_num($query) or safe_die("Couldn't get results<br />{$query}<br />" . $connect->ErrorMsg()); //Checked $num_fields = $result->FieldCount(); //This shouldn't occur, but just to be safe: if (count($fields) != $num_fields) { safe_die("Database inconsistency error"); } while (!$result->EOF) { $row = $result->GetRowAssoc(true); //Get assoc array, use uppercase reset($fields); //Jump to the first element in the field array $i = 1; foreach ($fields as $field) { $fieldno = strtoupper($field['sql_name']); if ($field['SPSStype'] == 'DATETIME23.2') { #convert mysql datestamp (yyyy-mm-dd hh:mm:ss) to SPSS datetime (dd-mmm-yyyy hh:mm:ss) format if (isset($row[$fieldno])) { list($year, $month, $day, $hour, $minute, $second) = preg_split('([^0-9])', $row[$fieldno]); if ($year != '' && (int) $year >= 1970) { echo "'" . date('d-m-Y H:i:s', mktime($hour, $minute, $second, $month, $day, $year)) . "'"; } else { echo $na; } } else { echo $na; } } else { if ($field['LStype'] == 'Y') { if ($row[$fieldno] == 'Y') { echo "'1'"; } else { if ($row[$fieldno] == 'N') { echo "'2'"; } else { echo $na; } } } else { if ($field['LStype'] == 'G') { if ($row[$fieldno] == 'F') { echo "'1'"; } else { if ($row[$fieldno] == 'M') { echo "'2'"; } else { echo $na; } } } else { if ($field['LStype'] == 'C') { if ($row[$fieldno] == 'Y') { echo "'1'"; } else { if ($row[$fieldno] == 'N') { echo "'2'"; } else { if ($row[$fieldno] == 'U') { echo "'3'"; } else { echo $na; } } } } else { if ($field['LStype'] == 'E') { if ($row[$fieldno] == 'I') { echo "'1'"; } else { if ($row[$fieldno] == 'S') { echo "'2'"; } else { if ($row[$fieldno] == 'D') { echo "'3'"; } else { echo $na; } } } } elseif (($field['LStype'] == 'P' || $field['LStype'] == 'M') && (substr($field['code'], -7) != 'comment' && substr($field['code'], -5) != 'other')) { if ($row[$fieldno] == 'Y') { echo "'1'"; } else { echo "'0'"; } } elseif (!$field['hide']) { $strTmp = mb_substr(strip_tags_full($row[$fieldno]), 0, $length_data); if (trim($strTmp) != '') { $strTemp = str_replace(array("'", "\n", "\r"), array("''", ' ', ' '), trim($strTmp)); /* * Temp quick fix for replacing decimal dots with comma's if (my_is_numeric($strTemp)) { $strTemp = str_replace('.',',',$strTemp); } */ echo "'{$strTemp}'"; } else { echo $na; } } } } } } if ($i < $num_fields && !$field['hide']) { echo ','; } $i++; } echo "\n"; $result->MoveNext(); } }