Jump to content

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


Big_Pat
Go to solution Solved by Psycho,

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?

Link to comment
Share on other sites

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 />";
}
Link to comment
Share on other sites

  • Solution

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

Edited by Psycho
Link to comment
Share on other sites

Fantastic, thanks Psycho! I'd moved on to using 

 

$countlogged=count(array_unique($logged_by));

 

and was *almost* there, but your query worked a charm. I'd never have got that. My MySQL queries are only on the basic level.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.