{{indexmenu_n>100}} ====== Form results to a CSV file ====== FIXME Translate CP form: With this hack you can save the form inputs in BE by using the ##download button## as a CSV file. \\ //([[http://de.wikipedia.org/wiki/CSV_(Dateiformat)]])// The solution presented here adds a CSV download link in the requested download output. \\ The URL can also be changed by hand in the URL bar of your browser. Change from \\ ##%%http://example.com/include/inc_act/act_export.php?action=exportformresult&fid=8%%## \\ to \\ ##%%http://example.com/include/inc_act/act_export.php?action=exportformresultcsv&fid=8%%## (Change exportformresult to exportformresult**csv**). After repeated send of this entry or the operation of the new link ## Download CSV file## a data download in CSV format is available. **Another solution without Hack** is the output by using the ##download button## and saving the generated source code in a file. This file you can read in as an HTML file, e.g. into Excel. \\ ===== Conditions ===== - In the form at the point "database": - The switch **[x]** save form results - The switch **[x]** save user profile data \\ ---- Docu: -- \\ Forum: [[http://forum.phpwcms.org/viewtopic.php?p=135673|Anfrage Änderung -Formular / Download-Button-]] **Author:** K.Heermann (flip-flop) http://planmatrix.de \\ //(17.05.2012)// **CMS Version:** >= 1.4 \\ **Version:** V1.0 17.05.2012 \\ **Update V1.1 18.05.2012:** A standard CSV output even if " and/or a field separator in the cell text, \\ see [[http://forum.phpwcms.org/viewtopic.php?p=135720#p135720]]. **Fileiname:** include/inc_act/act_export.php ---- \\ ==== Code snipped V1.1 (Hack) ==== **Update V1.1 18.05.2012:** A standard CSV output even if " and/or a field separator in the cell text, \\ see [[http://forum.phpwcms.org/viewtopic.php?p=135720#p135720]]. As a field separator the sign ##**;**## is in use, which is adjustable in the variable **$separator** around line 140. Enlagement of the file **include/inc_act/act_export.php** around line 120. \\ The first ##exit();## is replaced by: // +KH 17.05.12: ================================================================== // Dowload Link CSV-File echo '
Download CSV-File'.LF.'
'.LF; // ================================================================================ exit(); // +KH 17.05.12: ================================================================== } elseif($action == 'exportformresultcsv' && isset($_GET['fid']) && ($fid = intval($_GET['fid']))) { // $data = _getDatabaseQueryResult("SELECT *, DATE_FORMAT(formresult_createdate, '%Y-%m-%d %H:%i:%S') AS formresult_date FROM ".DB_PREPEND.'phpwcms_formresult WHERE formresult_pid='.$fid); $data = _dbQuery("SELECT *, DATE_FORMAT(formresult_createdate, '%Y-%m-%d %H:%i:%s') AS formresult_date FROM ".DB_PREPEND.'phpwcms_formresult WHERE formresult_pid='.$fid); if(!$data) die('Just a problem!'); $separator =';'; // CSV-Feldseparator $export = array(); $row = 1; $export[0] = array('#'=>'','#ID'=>'','#Date'=>'','#IP'=>''); // run all data first and combine array elements foreach($data as $key => $value) { // numbering starting at 1 $export[$row]['#'] = $row; $export[$row]['#ID'] = $value['formresult_id']; $export[$row]['#Date'] = $value['formresult_createdate']; $export[$row]['#IP'] = $value['formresult_ip']; $val_array = @unserialize($value['formresult_content']); if(is_array($val_array) && count($val_array)) { foreach($val_array as $a_key => $a_value) { $export[$row][$a_key] = $a_value; $export[0][$a_key] = ''; } } $row++; } header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: '.gmdate('D, d M Y H:i:s GMT', time()) ); header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0'); $filename = date('Y-m-d_H-i-s').'_formresultXmlID-'.$fid.'.csv'; header('Content-Type: text/comma-separated-values'); header( 'Content-Transfer-Encoding: BASE64;' ); header('Content-Disposition: attachment; filename="'.$filename.'"'); $elements = array(); // ---- Kopfzeile $elements[0] = ''; foreach($export[0] as $key => $value) { $elements[0] .= ''; $elements[0] .= $key; $elements[0] .= $separator; } $elements[0] = substr($elements[0], 0, -1); // letztes ";" in Zeile loeschen // ---- Naechste Zeilen for($x = 1; $x < $row; $x++) { $elements[$x] = ''; foreach($export[0] as $key => $value) { $elements[$x] .= ''; // if ( isset($export[$x][$key]) ) { // // Nicht zwingen notwenig, nur um fuer HTML-Darstellung // $export[$x][$key] = html_specialchars($export[$x][$key]); // Jede Spalte wird standardkonform in "..." gesetzt (OG: 18.05.2012) $export[$x][$key] = str_replace(chr(13),'', $export[$x][$key]); // CR loeschen wenn vorhanden (LF bleibt bestehen wenn vorhanden) $export[$x][$key] = '"'.str_replace('"', '""', $export[$x][$key]).'"'; // Jede Spalte in "..." setzen // } $elements[$x] .= $separator; // CSV-Trenner setzen } $elements[$x] = substr($elements[$x], 0, -1); // letztes ";" in Zeile loeschen unset($export[$x]); // free memory } echo implode(chr(13).chr(10), $elements); // CRLF am Zeilenende anfuegen exit(); // ================================================================================
\\ It is advisable to rename the original file to **example act_export_org.php** before doing the hack. Store the modified file e.g. under the name **act_export_patch.php**. This is useful when updating the system and the patched file **act_export.php** is overwritten. After an update this hack can be used again quite simple. \\