Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 450 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Altering Graph generated using PHP/MySQL

#1
I am looking at a very simple PHP code that uses our billing system's API to generate a graph. As of right now, it shows average ticket rating for every staff member. I would like to change it to instead just show the average ticket rating per month for the current year.

There is a 'date' field in the same table that has values formatted for each ticket like '2010-08-23 00:48:22'.

How would I alter this code to show average ticket ratings per month for the given year?

<?php

if (!defined("WHMCS"))
die("This file cannot be accessed directly");

$description = "This graph shows average support ratings by staff member";

if ($statsonly) { return false; }

$chartdata = array();

$query = "SELECT DATE_FORMAT(date, '%m-%Y') AS theMonth, AVG(rating) AS avgRating ";
$query .= "FROM tblticketreplies WHERE admin != '' AND rating !='0' ";
$query .= "GROUP BY DATE_FORMAT(date, '%m-%Y') ";
$query .= "ORDER BY avgRating ASC";
$result = mysql_query($query);

//$query = "SELECT admin, AVG(rating) AS avgrating FROM `tblticketreplies` WHERE admin != '' AND rating!='0' GROUP BY admin ORDER BY avgrating ASC";
//$result = mysql_query($query);

while ($data = mysql_fetch_array($result))
{
$chartdata[$data[0]] = round($data[1],2);
}

$graph=new WHMCSGraph(650,400);
$graph->addData($chartdata);
$graph->setTitle("Average Support Ticket Ratings by Month");
$graph->setGradient("lime", "green");
$graph->setDataValues(true);
$graph->setXValuesHorizontal(true);

?>
Reply

#2
You'll need to begin by altering your query to return an average rating per month, instead of an average rating per 'admin' as you have now. I don't know the name of the column in your database that has dates, but lets say it's named "MyDateColumn".

To get the monthly average, you'll need a query similar to:

$query = 'SELECT DATE_FORMAT(MyDateColumn, "%m-%Y") AS theMonth, AVG(rating) AS avgRating ';
$query .= 'FROM tblticketreplies ';
$query .= 'GROUP BY DATE_FORMAT(MyDateColumn, "%m-%Y") ';
$query .= 'ORDER BY avgRating ASC';

The [DATE_FORMAT][1] function is what's providing the magic. It's formatting 'MyDateColumn' as MM-YYYY (e.g. '06-2011'). Use the same format in the GROUP BY to complete the query.

Change the `setTitle` portion of your code to label the new data coming from your query:

$graph->setTitle("Average Support Ticket Ratings per Month");

Also, notice I removd the WHERE clause from your query. Before it was excluding 'ticketreplies' with an empty 'admin' or empty rating. You should evaluate adding this back into the query depending on what data you want to include in your chart.

Hope that's helpful.

~~
Edit:

Addressing the comments below - the query above sorts by `avgRating`, so your chart will always go from low to high rating value. To have it go from earliest month to most recent month, update the `ORDER BY`. Use `ORDER BY DATE_FORMAT(MyDateColumn, "%Y-%m")`. The `%Y-%m` is reversed here to ensure the sorting works correctly. The year is the most significant element here, so it goes first. The lesser significant month element goes last.

And lastly, to restrict your graph to only the current user, add a `WHERE` clause between the `FROM` clause and the `GROUP BY` clause. Use: `WHERE YEAR(MyDateColumn) = YEAR(CURDATE()) ` This instructs MySQL to only consider elements from your table when the year of the date in `MyDateColumn` is the same year as the year of the current date.

Do give the [MySQL date-time functions][1] a review. You'll thank yourself later. =)


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through