1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- session_start();
- if (!isset($_SESSION['hash_user'])) {
- header('Location: login.php');
- exit;
- }
- require_once('../controller/forms.php');
- $secret = $_SESSION['hash_user'];
- $info = getQuestionaireInfoBySecret($secret);
- $form_id = $info['qid'];
- header('Content-Type: text/csv; charset=utf-8');
- header('Content-Disposition: attachment; filename='.$info['title'].'.csv');
- $total_rows = get_total_rows($form_id);
- $res_data = get_records_pagination($form_id, 0, $total_rows);
- $output = fopen('php://output', 'w');
- $all_fields = array('id');
- foreach($res_data as $form) {
- foreach($form as $key => $val) {
- if(!in_array($key, $all_fields)) {
- $all_fields[] = $key;
- }
- }
- }
- fputcsv($output, $all_fields);
- foreach($res_data as $form) {
- $prepared = array();
- $prepared[] = $form->id;
- foreach($all_fields as $field) {
- if ($field == 'id') continue;
- if ($field === 'timestamp') {
- $prepared[] = date("H:i:s d/m/Y", $form->$field);
- continue;
- }
- if (isset($form->$field)) $prepared[] = $form->$field;
- else $prepared[] = '';
- }
- fputcsv($output, $prepared);
- }
- ?>
|