I'm trying to write a script that takes results from an SQL query and as it goes through each result it checks to see what the name of $row[13] should be and then Im wanting as it goes through each query result it identifies any names that are similar and keeps a count of how many times it sees that name. I thought I had a loop figured out that would count how many times the same name shows up in an array and then display it. The end result I am looking for is
Operations - 87
Training - 32
HR - 12
But after I run this code:
while($row = mysql_fetch_array($result_id))
{
$profile8 = mysql_query
("
SELECT org
FROM cost_centers
WHERE cost_center = '$row[13]'
")
or die(mysql_error());
$anymatches=mysql_num_rows($profile8);
while($pro8 = mysql_fetch_array($profile8))
{
$orgnames = $pro8[0];
}
$names[] = $orgnames;
$newArray = array_count_values($names);
foreach ($newArray as $key => $value) {
echo "$key - $value <br />";
}
}
I end up with it continuiously counting like this:
HR - 1
HR - 1
Communications - 1
HR - 1
Communications - 1
- 1
HR - 1
Communications - 1
- 2
HR - 1
Communications - 1
- 3
HR - 1
Communications - 1
- 4
HR - 2
Communications - 1
- 4
HR - 3
Communications - 1
- 4
HR - 3
Communications - 1
- 4
Operations - 1
HR - 4
Communications - 1
- 4
Operations - 1
HR - 4
Communications - 1
- 5
Operations - 1
HR - 4
Communications - 1
- 6
Operations - 1
HR - 4
Communications - 1
- 6
Operations - 2
HR - 4
Communications - 1
- 6
Operations - 3
HR - 4
So it almost looks like its grouping then counting, then regrouping as it keeps adding its count. I hope this makes sense and thanks in advance.