return round($earth_radius * $d); // Return the distance in meters between 2 GPS points
$d = 2 * atan2(sqrt($a), sqrt(1 - $a));
}
// Return the distance in meters between 2 GPS points
$connect=""; //Else PHP send an error, "connect don't exist", but that work anyway, this is just to remove a fake error
return round($earth_radius * $d);
$res=include "config.php"; //$res get the result of the calling of "config.php"
if ($res != 1){ //Check if config.php work
print (json_encode("Failed to connect to MySQL")); //Return a json string, so the dart script can interpret the error
}
if (!empty($_POST)) {
$id = $_POST['id']; //Get the result of the POST method in id
$query = 'SELECT * FROM gps'; //Browse all the database
$results = mysqli_query($connect, $query); //Execute the SQL command
$lat1 = 0; //Set $lat1 to test if the user exist
$lng1 = 0; //Set $lng1 to test if the user exist
$listUser = []; //Set the listUser to an empty list
while ($row = $results->fetch_row()){ //For all the row in the database
if (strcmp($row[0], $id) == 0) { //If the user is found in the database
$lat1 = $row[1]; //Set $lat1 to the latitude of the current user
$lng1 = $row[2]; //Set $lng1 to the longitude of the current user
}
}
if ($lat1 == 0 && $lng1 == 0) { //Check if the user get found in the database
print(json_encode("ERROR No user found in the database")); //Return a json string, so the dart script can interpret the error
exit(1); //Exit the actual script
}
$results = mysqli_query($connect, $query); //Execute again the SQL command to restart the fetch_row()
while ($row = $results->fetch_row()) { //For all the row in the database
if (strcmp($row[0], $id) != 0) { //If the row is not the row of the current user
$lat2 = $row[1]; //Set $lat2 to the latitude of the user who is in the actual row
$lng2 = $row[2]; //Set $lng2 to the latitude of the user who is in the actual row
$userID = $row[0]; //Set $userID to the username of the user who is in the actual row
$idMusic = $row[3]; //Set $idMusic to the id of the actual song of the user who is in the actual row
$dist = meters($lat1, $lng1, $lat2, $lng2); //With the function meters, calcul of the distance between the current user and the user who is in the actual row
if ($dist <= 100) { //If the user in the actual row is less than 100 meters away of the current user
$listUser[] = ['user' => $userID, 'music' => $idMusic]; //Add the username and the ID of the song that user who is in the actual row is listening
}
}
$connect="";
include "config.php";
$id = $_POST['id'];
$query = 'SELECT * FROM gps';
$stm = $connect->prepare($query);
$stm->execute();
$row = $stm->fetchAll(PDO::FETCH_ASSOC);
$lat1=0;
$lng1=0;
$listUser=[];
Foreach ($row as $col) {
if (strcmp($col['id'], $id) == 0) {
$lat1 = $col['latitude'];
$lng1 = $col['longitude'];
}
}
if ($lat1==0 && $lng1==0){
print(json_encode("ERROR No user found in the database"));