-
Notifications
You must be signed in to change notification settings - Fork 0
/
getfooddata.php
52 lines (44 loc) · 1.4 KB
/
getfooddata.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
<?php
// Gender, age and directional which submited by user
$gender = $_REQUEST["gender"];
$age = $_REQUEST["age"];
$dir = $_REQUEST["dir"];
// Database Configuration
$host='localhost';
$db = 'Food';
$username = 'postgres';
$password = 'postgres';
$dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
// Create a PostgreSQL database connection
$conn = new PDO($dsn);
// Query for extracting desired results based on selected options in distinct form
$queryStatement = 'SELECT DISTINCT(d.fgcat_id, d.fgid, s.servings, REPLACE(f.srvg_sz, \',\', \';\'), REPLACE(f.food, \',\', \';\')) FROM "foodgroups" As fg, "foods" As f, "serving" As s, "directional" As d WHERE d.directional = ' .$dir. ' AND s.ages = ' . $age . ' AND s.gender = ' .$gender . ' AND s.fgid = d.fgid AND f.fgcat_id = d.fgcat_id';
$q = $conn->query($queryStatement);
$results = $q->fetchAll();
// Explode results based on "," and rearry them again.
$i=0;
$output = array();
foreach ($results as $result)
{
$resarray = explode(",", $result[0]);
if($i==0)
{
$output[$i]= $resarray[2];
$i++;
$output[$i]= str_replace('"', "",$resarray[3]);
$i++;
$output[$i]= str_replace(')',"",$resarray[4]);
$i++;
}
else
{
$output[$i]= str_replace('"', "",$resarray[3]);
$i++;
$output[$i]= str_replace(')',"",$resarray[4]);
$i++;
}
}
// Form results as json
$outputJSON = json_encode($output);
echo $outputJSON;
?>