-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_heatmap.php
50 lines (47 loc) · 2.1 KB
/
get_heatmap.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
<?php
include("credentials.php");
$con = mysqli_connect($DB_HOST, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);
if (!$con)
{
echo mysqli_error();
die('Couldn\'t connect to the database');
}
else
{
$search_terms = "'" . implode("','", $_GET['search_terms']) . "'";
if(strcmp(gettype($_GET['search_times']), "array") == 0)
{
$search_times = $_GET['search_times'];
$query = "select max(cnt) from (select count(*) as cnt from Tweets where search_term IN ($search_terms) and (created_at BETWEEN '$search_times[0]' and '$search_times[1]') group by search_term, CAST(created_at as DATE)) counter;";
}
else
{
$query = "select max(cnt) from (select count(*) as cnt from Tweets where search_term IN ($search_terms) group by search_term, CAST(created_at as DATE)) counter;";
}
$results = mysqli_query($con, $query);
$row = mysqli_fetch_row($results);
$max_count = intval($row[0]);
// A big SQL injection can happen riiiiight here! Gotta prevent that later
// like maybe after the project is done!!
$term_counts = array();
foreach ($_GET['search_terms'] as $term)
{
if(strcmp(gettype($_GET['search_times']), "array") == 0)
{
$search_times = $_GET['search_times'];
$query = "select CAST(created_at as DATE), count(*) as cnt from Tweets where search_term = '$term' and (created_at BETWEEN '$search_times[0]' and '$search_times[1]') group by CAST(created_at as DATE);";
}
else
{
$query = "select CAST(created_at as DATE), count(*) as cnt from Tweets where search_term = '$term' group by CAST(created_at as DATE);";
}
$results = mysqli_query($con, $query);
$term_counts[$term] = array();
while ($row = mysqli_fetch_row($results))
{
$term_counts[$term][$row[0]] = intval($row[1]) / (float) $max_count;
}
}
echo json_encode($term_counts);
}
?>