export.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. session_start();
  3. if (!isset($_SESSION['hash_user'])) {
  4. header('Location: login.php');
  5. exit;
  6. }
  7. require_once('../controller/forms.php');
  8. $secret = $_SESSION['hash_user'];
  9. $info = getQuestionaireInfoBySecret($secret);
  10. $form_id = $info['qid'];
  11. header('Content-Type: text/csv; charset=utf-8');
  12. header('Content-Disposition: attachment; filename='.$info['title'].'.csv');
  13. $total_rows = get_total_rows($form_id);
  14. $res_data = get_records_pagination($form_id, 0, $total_rows);
  15. $output = fopen('php://output', 'w');
  16. $all_fields = array('id');
  17. foreach($res_data as $form) {
  18. foreach($form as $key => $val) {
  19. if(!in_array($key, $all_fields)) {
  20. $all_fields[] = $key;
  21. }
  22. }
  23. }
  24. fputcsv($output, $all_fields);
  25. foreach($res_data as $form) {
  26. $prepared = array();
  27. $prepared[] = $form->id;
  28. foreach($all_fields as $field) {
  29. if ($field == 'id') continue;
  30. if ($field === 'timestamp') {
  31. $prepared[] = date("H:i:s d/m/Y", $form->$field);
  32. continue;
  33. }
  34. if (isset($form->$field)) $prepared[] = $form->$field;
  35. else $prepared[] = '';
  36. }
  37. fputcsv($output, $prepared);
  38. }
  39. ?>