Jump to content

This is basic stuff, but..(multidimensional array count)


Big_Pat

Recommended Posts

Hi,

 

I'm trying to figure out the following. I know it must need a nested loop, I just can't figure out quite the way to do it, so any help would be superb.

 

My MySQL db has a list of incidents that a servicedesk team take in any given month. It outputs, among others, incident_number, logged_by and resolved_by.  The logged_by is, of course, an individual member of the servicedesk. That person MAY resolve the incident himself, so he MAY appear in the resolved_by field too.

My aim is to get the following:

 

SD member 1, calls logged, calls resolved.

SD member 2, calls logged, calls resolved.

etc

 

Can someone help, please?

Oh, sorry. 

Well, I got a bit confused in my nested loops, you see, so it wasn't making a lot of sense.

 

Basically, I started with this but got lost.

$countlogged=count($logged_by);
$countresolved=count($resolved_by);
for ($i=0; $i<$countlogged;$i++){
    for ($j=0;$j<$countresolved;$j++){
     if ($resolved_by[$j] == $logged_by[$i]){
      $resolved_sum[$i]= $resolved_sum[$i] +1;
     }
     $logged_sum[$i] = $logged_sum[$i] +1;
    } //closes loop of resolvedbys
}//closes loop of loggedbys
for ($i=0; $i<$countlogged;$i++){
    echo $logged_sum[$i] . " - " . $resolved_sum[$i] ."<br />";
}

If the data is in a database, you can just calculate the values using a query.

SELECT member, SUM(logged) as logged_count, SUM(resolved) as resolved_count
FROM
(
    SELECT logged_by as member, COUNT(incident_number) as logged, 0 as resolved
    FROM table_name
    GROUP BY logged_by
    UNION
    SELECT resolved_by as member, 0 as logged, COUNT(incident_number) as resolved
    FROM table_name
    GROUP BY resolved_by
) as results

GROUP BY member

EDIT: Corrected query

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.