Advertisement
Guest User

Untitled

a guest
Jul 29th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. public function convert_csv($csv, $json)
  2. {
  3. /*
  4. * Open files.
  5. */
  6. $csv_handle = fopen($csv, 'r');
  7. $json_handle = fopen($json, "w");
  8. /*
  9. * Get the table headers.
  10. */
  11. $headers = fgetcsv($csv_handle);
  12. /*
  13. * Write the array name.
  14. */
  15. fwrite($json_handle, ""LogData":[");
  16.  
  17. $FirstRecord = true;
  18. while ($row = fgetcsv($csv_handle))
  19. {
  20. /*
  21. * Ensure no trailing comma after last record.
  22. */
  23. if (!$FirstRecord)
  24. fwrite($json_handle, ",");
  25. else
  26. $FirstRecord = false;
  27. /*
  28. * Create JSON record and write to file.
  29. */
  30. $complete = array_combine($headers, $row);
  31. fwrite($json_handle, json_encode($complete));
  32. }
  33. /*
  34. * Close the array
  35. */
  36. fwrite($json_handle, "]");
  37. /*
  38. * Close the files.
  39. */
  40. fclose($csv_handle);
  41. fclose($json_handle);
  42. }
  43.  
  44. TimeStamp,Value
  45. 1390364805600.01,2.0
  46. 1390451205600.01,3.0
  47. 1390537605600.01,0.5
  48. 1390546245600.02,23.0
  49. 1390563525599.99,0.8
  50.  
  51. "LogData":[{"TimeStamp":"1390364805600.01","Value":"2.0"},{"TimeStamp":"1390451205600.01","Value":"3.0"},{"TimeStamp":"1390537605600.01","Value":"0.5"},{"TimeStamp":"1390546245600.02","Value":"23.0"},{"TimeStamp":"1390563525599.99","Value":"0.8"}]
  52.  
  53. $this->logging_model->convert_csv($path, "/tmp/log.json");
  54. //header('Content-Type: application/json'); // I have tried with and without this line
  55. readfile("/tmp/log.json"); // push it out
  56.  
  57. $.ajax({
  58. url: URL,
  59. type: "GET",
  60. dataType: "json",
  61. success: function(data) { SaveData(PointId, data);},
  62. error: function (request, status, error) { alert(error);},
  63. async: false
  64. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement