Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ob_start(); // Allows the session header to be cached
- $GLOBALS['server'] = "http://localhost/tests/Tuto_OCR/SailingRobotsWebsite/";
- //$GLOBALS['server'] = "http://www.sailingrobots.com/testdata/";
- $GLOBALS['username'] = 'root';
- //$GLOBALS['username'] = 'ithaax_testdata';
- $GLOBALS['password'] = '';
- //$GLOBALS['password'] = 'test123data';
- $GLOBALS['hostname'] = '127.0.0.1';
- $GLOBALS['database_name_testdata'] = 'ithaax_testdata';
- $GLOBALS['database_name'] = 'ithaax_website_config';
- $GLOBALS['database_ASPire'] = 'ithaax_ASPire_config';
- $GLOBALS['database_mission'] = 'ithaax_mission';
- $hostname = $GLOBALS['hostname'];
- $username = $GLOBALS['username'];
- $password = $GLOBALS['password'];
- $dbname = $GLOBALS['database_ASPire'];
- try
- {
- $db = new PDO("mysql:host=$hostname;dbname=$dbname;charset=utf8;port=3306",
- $username,
- $password,
- array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
- );
- }
- catch(Exception $e)
- {
- die('Error : '.$e->getMessage());
- }
- $GLOBALS['db_connection'] = $db;
- $db = $GLOBALS['db_connection'];
- $data='{"dataLogs_actuator_feedback":[{"id":"155","rc_on":"0","rudder_position":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wingsail_position":"-2000.0"}],"dataLogs_compass":[{"heading":"123.0","id":"155","pitch":"0.0","roll":"0.0","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_course_calculation":[{"bearing_to_waypoint":"98.0","course_to_steer":"142.1232758","distance_to_waypoint":"1784.544189","going_starboard":"1","id":"155","t_timestamp":"2018-04-25 05:51:03.861","tack":"0"}],"dataLogs_current_sensors":[{"actuator_unit":"-2000.0","id":"155","navigation_unit":"-2000.0","sailboat_drive":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wind_vane_clutch":"-2000.0"}],"dataLogs_gps":[{"course":"123.0","has_fix":"1","id":"155","latitude":"60.10724258","longitude":"19.92242813","online":"1","route_started":"0","satellites_used":"0","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861","time":"2018-04-25 05:51:03.861"}],"dataLogs_marine_sensors":[{"conductivity":"1.171875","id":"155","ph":"7.171875","salinity":"0.01003836561","t_timestamp":"2018-04-25 05:51:03.861","temperature":"20.171875"}],"dataLogs_system":[{"actuator_feedback_id":"155","compass_id":"155","course_calculation_id":"155","current_mission_id":"1501671669","current_sensors_id":"155","gps_id":"155","id":"155","marine_sensors_id":"155","vessel_state_id":"155","wind_state_id":"155","windsensor_id":"155"}],"dataLogs_vessel_state":[{"course":"123.0","heading":"123.0","id":"155","latitude":"60.10724258","longitude":"19.92242813","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_wind_state":[{"apparent_wind_direction":"147.0","apparent_wind_speed":"2.947221518","id":"155","t_timestamp":"2018-04-25 05:51:03.861","true_wind_direction":"270.6591797","true_wind_speed":"3.000576973"}],"dataLogs_windsensor":[{"direction":"147.0","id":"155","speed":"2.947221518","t_timestamp":"2018-04-25 05:51:03.861","temperature":"21.0"}]}';
- $data = json_decode($data,true);
- if (!empty($data))
- {
- error_log("GOT HERE");
- foreach ($data as $table_name => $table)
- {
- // Generate the array to be bind with the prepared SQL query
- foreach ($table as $id_log => $log)
- {
- $param_stmt = "(";
- $param_to_fill = "(";
- $param_array = array();
- foreach ($log as $column_name => $value)
- {
- $param_array[$column_name] = $value;
- $param_stmt = $param_stmt . ''.$column_name .',' ;
- $param_to_fill = $param_to_fill . ':'.$column_name.',';
- }
- // Remove the extra comma
- $param_stmt = substr($param_stmt, 0, -1).')';
- $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
- $param_array['id'] = NULL;
- // Prepare the SQL Query
- $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
- $query->execute($param_array);
- }
- }
- //$result = array('result' => 1);
- //return json_encode($result);
- }
- $data='{"dataLogs_actuator_feedback":[{"id":"155","rc_on":"0","rudder_position":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wingsail_position":"-2000.0"}],"dataLogs_compass":[{"heading":"123.0","id":"155","pitch":"0.0","roll":"0.0","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_course_calculation":[{"bearing_to_waypoint":"98.0","course_to_steer":"142.1232758","distance_to_waypoint":"1784.544189","going_starboard":"1","id":"155","t_timestamp":"2018-04-25 05:51:03.861","tack":"0"}],"dataLogs_current_sensors":[{"actuator_unit":"-2000.0","id":"155","navigation_unit":"-2000.0","sailboat_drive":"-2000.0","t_timestamp":"2018-04-25 05:51:03.861","wind_vane_angle":"-2000.0","wind_vane_clutch":"-2000.0"}],"dataLogs_gps":[{"course":"123.0","has_fix":"1","id":"155","latitude":"60.10724258","longitude":"19.92242813","online":"1","route_started":"0","satellites_used":"0","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861","time":"2018-04-25 05:51:03.861"}],"dataLogs_marine_sensors":[{"conductivity":"1.171875","id":"155","ph":"7.171875","salinity":"0.01003836561","t_timestamp":"2018-04-25 05:51:03.861","temperature":"20.171875"}],"dataLogs_system":[{"actuator_feedback_id":"155","compass_id":"155","course_calculation_id":"155","current_mission_id":"1501671669","current_sensors_id":"155","gps_id":"155","id":"155","marine_sensors_id":"155","vessel_state_id":"155","wind_state_id":"155","windsensor_id":"155"}],"dataLogs_vessel_state":[{"course":"123.0","heading":"123.0","id":"155","latitude":"60.10724258","longitude":"19.92242813","speed":"0.06338219345","t_timestamp":"2018-04-25 05:51:03.861"}],"dataLogs_wind_state":[{"apparent_wind_direction":"147.0","apparent_wind_speed":"2.947221518","id":"155","t_timestamp":"2018-04-25 05:51:03.861","true_wind_direction":"270.6591797","true_wind_speed":"3.000576973"}],"dataLogs_windsensor":[{"direction":"147.0","id":"155","speed":"2.947221518","t_timestamp":"2018-04-25 05:51:03.861","temperature":"21.0"}]}';
- $data = json_decode($data,true);
- if (!empty($data))
- {
- $idMap = array();
- $dataLogs_system = $data['dataLogs_system'];
- unset($data['dataLogs_system']);
- //print_r($data);
- foreach ($data as $table_name => $table)
- {
- // Generate the array to be bind with the prepared SQL query
- foreach ($table as $id_log => $log)
- {
- $param_stmt = "(";
- $param_to_fill = "(";
- $param_array = array();
- foreach ($log as $column_name => $value)
- {
- $param_array[$column_name] = $value;
- $param_stmt = $param_stmt . ''.$column_name .',' ;
- $param_to_fill = $param_to_fill . ':'.$column_name.',';
- }
- // Remove the extra comma
- $param_stmt = substr($param_stmt, 0, -1).')';
- $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
- $param_array['id'] = NULL;
- // Prepare the SQL Query
- $query = $db->prepare("INSERT INTO $table_name $param_stmt VALUES $param_to_fill ;");
- $query->execute($param_array);
- $trueTableNameStartPos = strpos($table_name, 'dataLogs_');
- if ($trueTableNameStartPos !== false) {
- $sql= "SELECT MAX(id) FROM $table_name";
- $result = $db->query($sql);
- $trueTableNameStart = substr($table_name, $trueTableNameStartPos);
- $idMap[$trueTableNameStart] = $result->fetch()['MAX(id)'];
- }
- }
- }
- //print_r($idMap);
- foreach ($idMap as $column_name => $value)
- {
- $dataLogs_system[$column_name."_id"] = $value;
- }
- $param_stmt = "(";
- $param_to_fill = "(";
- $param_array = array();
- print_r($idMap);
- echo "<br><br><br>";
- print_r($dataLogs_system[0]);
- echo "<br><br><br>";
- foreach ($dataLogs_system[0] as $column_name => $value)
- {
- $param_array[$column_name] = $value;
- $param_stmt = $param_stmt . ''.$column_name .',' ;
- $param_to_fill = $param_to_fill . ':'.$column_name.',';
- }
- // Remove the extra comma
- $param_stmt = substr($param_stmt, 0, -1).')';
- $param_to_fill = substr($param_to_fill, 0, -1).')';// Now whe hace something like (?, ?, ?, ?)
- $param_array['id'] = NULL;
- // Prepare the SQL Query
- echo "INSERT INTO dataLogs_system $param_stmt VALUES $param_to_fill ;";
- echo "<br><br><br>";
- print_r($param_array);
- $query = $db->prepare("INSERT INTO dataLogs_system $param_stmt VALUES $param_to_fill ;");
- $query->execute($param_array);
- //$result = array('result' => 1);
- //return json_encode($result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement