I am collecting ADSB data on a Raspberry Pi via a JSON file; I am using someones PHP code that is producing a SQL data base from the JSON file.However I need a csv file to feed into an analsis program (SPSS) that I have developed. Here is the PHP code that creates the sql database
// generate sql insert statement per aircraft in range of user set alt_geom/latitude/longitude and optionally according only to hex or flight numbers in hex_code_array.txt and flight_code_array.txt
#var_dump($hex_code_array); var_dump($flight_code_array); // show arrays for debug
if ($user_set_array['filter_mode_database'] && $user_set_array['filter_mode_database_limited']) {
if (($ac_alt_geom != '' && $ac_alt_geom < $user_set_array['max_alt'] && $ac_lat < $user_set_array['max_lat'] && $ac_lat > $user_set_array['min_lat'] && $ac_lon < $user_set_array['max_lon'] && $ac_lon > $user_set_array['min_lon']) && (func_wildcard_search($ac_hex, $hex_code_array, $user_set_array['filter_mode_wildcard']) || ($ac_flight != '' && func_wildcard_search($ac_flight, $flight_code_array, $user_set_array['filter_mode_wildcard'])))) {
$sql .= "INSERT INTO aircrafts VALUES (NULL, '" . date("Y-m-d G:i:s l", $ac_now) . "', '$ac_now', '$ac_hex', '$ac_flight', '$ac_dist', ";
$sql .= "'$ac_alt_geom', '$ac_lat', '$ac_lon', '$ac_track', '$ac_gs', '$ac_baro_rate', '$ac_seen_pos', '$ac_seen', ";
$sql .= "'$ac_rssi', '$ac_messages', '$ac_category', '$ac_squawk', '$ac_alt_baro', '$ac_mlat', '$ac_tisb', '$message_rate');";
$sql .= PHP_EOL;
As I am going up a steep learning curve with PHP I have adapted some code that I found else where on Stack but its not producing a csv file. My objective was to take the variables that create the sql arrays and continually update the csv file .
$data= "id,now,hex,flight,distance,altitude,lat,long,track,gs,baro-rate,seen-pos,seen,rasi,messages,category,squark,alt-baro,mlat,tisb,rec-mag-sec\n";
$i = 1;
#echo $data;
$data = array(
array( '$i' ),
array( '$ac_id' ),
array( '$ac_now' ),
array( '$ac_hex' ),
array( '$ac_flight' ),
array( '$ac_distance' ),
array( '$ac_alt_geom' ),
array( '$ac_lat' ),
array( '$ac_long' ),
array( '$ac_track' ),
array( '$ac_gs' ),
array( '$ac_baro_rate' ),
array( '$ac_seen_pos' ),
array( '$ac_seen' ),
array( '$ac_rssi' ),
array( '$ac_messages' ),
array( '$ac_category' ),
array( '$ac_squark' ),
array( '$ac_alt_baro' ),
array( '$ac_mlat' ),
array( '$ac_tisb' ),
array( '$ac_rec_mag_sec' ),
);
}
function outputCSV($data) {
$outputBuffer = fopen("php://output", 'w');
foreach($data as $val) {
fputcsv($outputBuffer, $val);
}
fclose($outputBuffer);
}
$filename = 'aircrafts';
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=($filename).csv");
header("Pragama: no-cache");
hwader("Expires: 0");
outputCSV($data);