I am trying to format my own data (which currently is just displayed in various tables and KPI indicator blocks) into the format required by a graphing framework. In essence, I want some of my values to become the X and Y data for a graph generating script.
This is the desired outcome (an example of an array that works generating a graph/graphpoints from):
$dataPoints = array(
array("x"=> 1, "y"=> 41, label=> "18/07/19"),
array("x"=> 2, "y"=> 35, label=> "19/07/19"),
array("x"=> 3, "y"=> 50, label=> "20/07/19"),
array("x"=> 4, "y"=> 45, label=> "21/07/19"),
array("x"=> 5, "y"=> 52, label=> "22/07/19"),
array("x"=> 6, "y"=> 68, label=> "23/07/19")
);
I have the following code that pulls the relevant information I need:
//Build Graph Data Points
//
$daysmax=31;
$daycounter=0;
while($daysmax > $daycounter) {
$sql_get_day_total_events="SELECT COUNT(*),date_sub(curdate(),INTERVAL $daycounter day) as date1 FROM tblticketlog
WHERE DATE(date) = date_sub(curdate(),INTERVAL $daycounter day)
AND ((action = 'New Support Ticket Opened')
OR (action LIKE 'Status changed to Pending%')
OR (action LIKE 'Status changed to In Progress%')
OR (action LIKE 'New Ticket Response made by%'))";
$get_day_total_events_result=mysqli_query($db,$sql_get_day_total_events);
if(mysqli_num_rows($get_day_total_events_result) > 0) {
while($row = $get_day_total_events_result->fetch_assoc()) {
$day_result_date=strtotime($row['date1']);
$day_result_value=$row['COUNT(*)'];
}
}
$sql_get_day_escs="SELECT COUNT(*) FROM escalations
WHERE DATE(esc_at) = date_sub(curdate(), INTERVAL $daycounter day)
AND escalated = 1";
$get_day_escs_result=mysqli_query($db2,$sql_get_day_escs);
if(mysqli_num_rows($get_day_escs_result) > 0) {
while($row = $get_day_escs_result->fetch_assoc()) {
$day_escs_value=$row['COUNT(*)'];
}
}
$day_slas_met = $day_result_value - $day_escs_value;
$day_kpi_pcnt = round((($day_slas_met / $day_result_value) * 100),0);
$daycounter=$daycounter + 1;
}
So to summarise that, I pull the total events per day for 30 days (rolling) from $db, I pull the number of "escalation events" per day for 30days (rolling) form $db2. And I also grab the date from the $db query too.
For my graph:
X = $daycounter (just an incremental numeric ID for the data point, starting form 0 and going to 30
Y = The KPI Percentage, which is just worked out using simple math in $day_kpi_pcnt
LABEL = The plain text formatted date object
What I tried to do
I initialized the $datapoints array before my loop with:
$datapoints = array();
Then just before the $daycounter+1 line at the bottom of the loop I tried to push my values to the indexes in the array:
$datapoints[x] = $daycounter;
$datapoints[label] = date("d-m-Y",$day_result_date);
$datapoints[y] = $day_kpi_pcnt;
However, this just ends up with an empty graph. If I var_dump the array, I seem to get the last line of my results in an otherwise empty array:
**Day: 30 -- 29-06-2019 - 6/6 - 100%**
array(3) { ["x"]=> int(30) ["label"]=> string(10) "29-06-2019" ["y"]=> float(100) }
I'll be honest, PHP arrays and how to use them is probably my weakest subject. I really struggle with them, and would appreciate some guidance or even some basic tips on where I've gone wrong. Thanks.