-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
65 lines (62 loc) · 2.04 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
$servername = "cse.unl.edu";
$username = "sbhandari";
$password = "*********************";
$dbname = "sbhandari";
// Get values from ajax
$year = $_POST['val'];
$livestock_string = $_POST['livestock'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create query to get countries and data
$countryQ = "SELECT country, sum( year".$year." ) as sum_year".$year." FROM dwfi_data WHERE year".$year." != 0 and country!='World Total' and commodity IN ". $livestock_string ." group by country;" ;
// Create query to get geolocations
$geoQ = "SELECT latitude, longitude FROM sbhandari.countries WHERE latitude != 0;";
$countryRS = $conn->query($countryQ);
$geoRS = $conn->query($geoQ);
// Parse result into array and then json
if ($countryRS->num_rows > 0) {
// output data of each row
$finalArray[0] = array('Country', 'Livestock Exported (million tons)');
$i = 1;
while($row = $countryRS->fetch_assoc()) {
$array = array(
$row["country"] , (int)$row["sum_year".$year],
);
$finalArray[$i] = $array;
$i = $i + 1;
}
// convert this into a JSON object
// then use $finalArray then echo json_encode(whatever)
$countryJson = json_encode($finalArray, JSON_PRETTY_PRINT);
echo $countryJson;
} else {
echo "0 results";
}
// Parse result into array and then json
if ($geoRS->num_rows > 0) {
// output data of each row
//$finalArray[0] = array('Country', 'Livestock Exported (million tons)');
$i = 1;
while($row = $geoRS->fetch_assoc()) {
$array2 = array(
(double)$row["latitude"] , (double)$row["longitude"],
);
$finalArray2[$i] = $array2;
$i = $i + 1;
}
$geoJson = json_encode($finalArray2, JSON_PRETTY_PRINT);
var featuresText = '{"features":[{"latitude", "longtitude"}]}';
var formatter = new OpenLayers.Format.GeoJSON();
var features = formatter.read(featuresText);
console.log(features);
// echo $geoJson;
} else {
echo "0 results";
}
$conn->close();
?>