I hope you are all well.
I am currently teaching myself PHP and as part of this I have been attempting to develop a feedback system that stores the info off customer feedback cards for the restaurant where I work.
The problem that I have revolves around the php code that I am using to generate the json output that is then used by jqplot to create a graph.
Here is the code I am using to do this:
<?php
// $toDate and $fromDate will eventually be values coming over via post however i am using static values at the moment for testing
$toDate = time();
$fromDate = time() - (7 * 24 * 60 * 60);
$sql = "SELECT * FROM feedback WHERE visitDateTime BETWEEN FROM_UNIXTIME($fromDate) AND FROM_UNIXTIME($toDate)";
$result = mysql_query($sql) or die(mysql_error());
?>
After getting the data I then use this code in javascript and php to generate the jqplot data.
<script>
$(document).ready(function(){
var line1 = [
<?php
while ($row = mysql_fetch_assoc($result)){
echo '[';
echo "'";
echo $row['visitDateTime'];
echo "',";
echo $row ['foodScore'];
echo ']';
echo ',';
}
?>
];
// code then goes on to generate the chart, etc....
</script>
This works fine and generates the following json output:
var line1 = [
['2012-04-20 19:00:00',7],['2012-04-20 19:00:00',8],['2012-04-20 12:00:00',8],['2012-04-22 16:00:00',5],['2012-04-21 20:00:00',9],
];
However, the problem that i have is that when 2 dates and times are the same, which they probably would be, then this causes problems on the chart because i have multiple values for 1 date.
After giving it some thought i came to the conclusion that what i need to actually be doing is calculating the average score for the dates and times so that it generates output like shown below:
var line1 = ['unique date/time', average of all scores from this date/time ]
But i have no idea how to even begin to do that.
I know this is probably wrong but im thinking that i need to loop through all the retrieved records and check to see if the date/time is the same. So basically, instead of returning this:
['2012-04-20 19:00:00',7],['2012-04-20 19:00:00',8],['2012-04-20 12:00:00',8],['2012-04-20 12:00:00',5],['2012-04-20 12:00:00',9]
I need to return this:
['2012-04-20 19:00:00',7.5],['2012-04-20 12:00:00',7.3]
If anyone could offer me any pointers on how to go about this it would really help me out.
Thanks everyone.
Instead of doing the averaging calculations on the server, you could let MySQL do it for you. You could use a query like this to retrieve the data grouped and averaged directly from MySQL
Let me know if this doesn’t work