Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:43:38+00:00 2026-05-23T08:43:38+00:00

I am looking at a very simple PHP code that uses our billing system’s

  • 0

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);

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:43:39+00:00Added an answer on May 23, 2026 at 8:43 am

    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 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 a review. You’ll thank yourself later. =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a very simple example of active record in .NET (code samples
I'm looking for something that can interpret a string in php and perform simple
im looking easy steps for convert any html/php template into a very simple wordpress
I'm looking for a very simple PHP email reader (I use reader loosely -
I'm looking for a very simple tool to monitor the bandwidth of all my
In any language really, im looking for a simple (very simple) way to control
I'm looking for a very specific eclipse plugin that will tell me if a
I have this very simple script that allows the user to specify the url
I am not very experienced with PHP. I have a site I'm maintaining that
I'm not very experienced when it comes to XML, I'm looking for a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.