I need to generate statistics for my ticketing system.
I have a database called tickets
, where everything is linked to other tables using the primary keys of these.
When a technician is assigned a ticket, it specifies which company to visit. This field is called project_id
.
Now, I want to know which are the 5 companies that have the most problems, and how many tickets have been opened for them. I tried this and this and it helps me to tell you that there are 22 companies (out of a total of 90) that had problems and opened tickets.
As I only occupy the first five, I wrote this function.
public static function getAllProjectsWithProblems(){
$sql = "select project_id, count(*) as c from ".self::$tablename." group by project_id having count(project_id)>=1 order by c desc limit 5";
$query = Executor::doit($sql);
return Model::many($query[0],new TicketData());
}
That helps me show them in descending order according to the number of tickets they have opened. But, it only does that. It does not allow me to know how many tickets they opened.
This is an example of my database
id -------------- description ------- project_id
1 -------------- This is an example ------- 1
2 -------------- This is an example ------- 1
3 -------------- This is an example ------- 1
4 -------------- This is an example ------- 1
5 -------------- This is an example ------- 2
6 -------------- This is an example ------- 2
7 -------------- This is an example ------- 2
8 -------------- This is an example ------- 3
9 -------------- This is an example ------- 4
10 -------------- This is an example ------- 4
11 -------------- This is an example ------- 4
12 -------------- This is an example ------- 4
13 -------------- This is an example ------- 4
14 -------------- This is an example ------- 4
15 -------------- This is an example ------- 5
Result I occupy:
Empresas con problemas: (5)
Empresa 4 = 6 issues
Empresa 1 = 4 issues
Empresa 2 = 3 issues
Empresa 3 = 1 issue
Empresa 5 = 1 issue
This is what I manage to generate with my code:
Empresas con problemas: (5)
Empresa 4
Empresa 1
Empresa 2
Empresa 3
Empresa 5
This is the HTML:
<div class="card" style="position: relative; left: 0px; top: 0px;">
<div class="card-header ui-sortable-handle" style="cursor: move;">
<h3 class="card-title">
<i class="ion ion-clipboard mr-1"></i>
Empresas con problemas: (<?php echo count(TicketData::getAllProjectsWithProblems());?>)
</h3>
</div>
<div class="card-body">
<ul class="todo-list ui-sortable">
<?php
$users = array();{
$users = TicketData::getAllProjectsWithProblems();
}
if (count($users) > 0) {
// count if there are users
?>
<?php
foreach ($users as $user) {
$project = $user->getProject();
?>
<li>
<span class="text"> <?php echo$project->id; ?>: //acá debería ir el número de tickets que se abrieron// </span>
</li>
<?php } ?>
<?php
} else {
echo "<p class='alert alert-danger'>You don't have open tickets.</p>";
}
?>
</ul>
</div>
In what part of the code am I omitting the extraction of the total number of tickets that each company has? Or, how can I do a function that allows me to count using the id that was extracted?
I have this idea, but I do not know how to make the function take the value that is printed in <span class="text"> <?php echo$project->id; ?>: //acá debería ir el número de tickets que se abrieron// </span>
.
public static function CountProblems(){
$sql = "select project_id, count(*) as e from ".self::$tablename." where project_id like $this->project_id";
$query = Executor::doit($sql);
return Model::many($query[0],new TicketData());
}