I have a heat map application on OpenLayers (using XAMPP 1.8.0 and MySQL) where user enters two date and time intervals and selects whether he/she would like to view all the data between those two datetimes or view them based on hourly/daily/weekly/monthly intervals. However, when I type in two date and times as input and try to visualize the data, I can only visualize all the data between those two intervals. Whenever I choose hourly, daily, weekly etc from my HTML drop down menu and press fetch queries button, I get an Error in Ajax POST message. What could be the cause here? The codes are below:
The HTML Post part:
$(document).ready(function(){
// ajaxForm submission
$('#ajaxForm').submit(function() {
$.ajax({
type: 'POST',
url: 'heatQuery.php',
//url: 'http://localhost/heatQuery.php',
data: $(this).serialize(),
dataType: 'json',
success: function(response)
{
// update the points for heatmap layer
updateHeatMap(response);
},
error: function(errorMsg)
{
alert(errorMsg);
}
});
return false;
});
});
The PHP Part:
setupDB.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "myuser";
$mysql_password = "mypassword";
$mysql_database = "mydatabase";
$bd = mysql_connect($mysql_hostname, $mysql_user) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");
?>
heatQuery.php
<?php
include 'setupDB.php';
$coords_array = array();
function getPoints($dateTimeBeg,$dateTimeEnd)
{
$ses_sql= "SELECT lat,lon FROM `mytable` WHERE calltime >= '$dateTimeBeg' AND calltime <= '$dateTimeEnd'";
$result = mysql_query($ses_sql,$bd) or die('Error: ' . mysql_error());
if($result)
{
$num_of_rows = mysql_num_rows($result);
if($num_of_rows > 0)
{
while($row = mysql_fetch_row($result))
{
array_push($coords,array($row[0],$row[1]));
}
array_push($coords_array,$coords);
}
}
}
$hours = array(1 => '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '24:00');
$dateBeg = isset($_POST['date1']) ? $_POST['date1'] : "";
$dateEnd = isset($_POST['date2']) ? $_POST['date2'] : "";
$dateFreq = isset($_POST['freq']) ? $_POST['freq'] : "";
if($dateBeg && $dateEnd && $dateFreq) // if all variables are set
{
$datetime1 = new DateTime($dateBeg);
$datetime2 = new DateTime($dateEnd);
$interval = $datetime1->diff($datetime2,true);
if($dateFreq == "all")
{
$coords = array();
$timeBeg = $_POST['time1'];
$timeEnd = $_POST['time2'];
$sp = " ";
$dateTimeBeg = $dateBeg.$sp.$timeBeg;
$dateTimeEnd = $dateEnd.$sp.$timeEnd;
$ses_sql= "SELECT lat,lon FROM `mytable` WHERE calltime >= '$dateTimeBeg' AND calltime <= '$dateTimeEnd'";
$result = mysql_query($ses_sql,$bd) or die('Error: ' . mysql_error());
if($result)
{
$num_of_rows = mysql_num_rows($result);
if($num_of_rows > 0)
{
while($row = mysql_fetch_row($result))
{
array_push($coords,array($row[0],$row[1]));
}
array_push($coords_array,$coords);
}
}
}else if($dateFreq == "hourly")
{
for($i=1;$i<=24;$i+=1)
{
$coords = array();
$timeBeg = $hours[$i];
$timeEnd = $hours[$i+1];
$dateTimeBeg = $dateBeg.$sp.$timeBeg;
$dateTimeEnd = $dateBeg.$sp.$timeEnd;
$ses_sql= "SELECT lat,lon FROM `mytable` WHERE calltime >= '$dateTimeBeg' AND calltime <= '$dateTimeEnd'";
$result = mysql_query($ses_sql,$bd) or die('Error: ' . mysql_error());
if($result)
{
$num_of_rows = mysql_num_rows($result);
if($num_of_rows > 0)
{
while($row = mysql_fetch_row($result))
{
array_push($coords,array($row[0],$row[1]));
}
array_push($coords_array,$coords);
}
}
}
}else if($dateFreq == "daily")
{
for($i=1;$i<=interval;$i+=1)
{
$ses_sql= "SELECT lat,lon FROM `mytable` WHERE calltime >= '$dateTimeBeg' AND calltime <= '$dateTimeEnd'";
$result = mysql_query($ses_sql,$bd) or die('Error: ' . mysql_error());
if($result)
{
$num_of_rows = mysql_num_rows($result);
if($num_of_rows > 0)
{
while($row = mysql_fetch_row($result))
{
array_push($coords,array($row[0],$row[1]));
}
array_push($coords_array,$coords);
}
}
}
}
$coords_json = json_encode($coords_array);
echo $coords_json;
//echo $coords;
mysql_close($bd);
}
?>