How to count equal records in a column and get the same number?

Asked

Viewed 26 times

1

Table example:

| Ti | Orien      |

| ---|   JML      |

| ---|   JML      |

| ---|   RGM      |

| ---|   AAA      |

| ---|   AAA      |

Result example: JML - 2 RGM - 1 AAA - 2

1 answer

1

With php5.5 can combine function array_column() that extract the input values and return them as a new array, with use of the function array_count_values() that will tell you how many equal values there are in the array, the keys are the database values and the values are the number of occurrences.

$arr = [['str' => 'abc'], ['str' => 2015], ['str' => 2015], ['str' => 'doge'], ['str' => 'wow'], ['str' => 'wow']];
$itens = array_count_values(array_column($arr, 'str'));

echo "<pre>";
print_r($itens);

Return:

Array
(
    [abc] => 1
    [2015] => 2
    [doge] => 1
    [wow] => 2
)
  • I was thinking something like a Select Ti, Count(Orien) as in a FROM table".

  • @Ruimanuelmartins , a select Ti, count(Orien) as num FROM tabela group by Orien resolves?

  • I haven’t tested it yet. This is my code:

  • $query_b="SELECT * FROM docentes ORDER BY docentes.NomeAscii ASC"; $query_c= "SELECT Advisor, COUNT() AS numInstances FROM theses GROUP BY Advisor"; print_TD("<select name='orientarisep'></option>"; // list box select command echo "<option value=' '</option>" ." Invite advisor"; foreach ($db->query($query_b) as $Row)//Array or Records stored in $Row { echo "<option value=$Row[Coddocente]>$Row[Coddocente] - $Row[Nomescii]</option>"; / Option values are Added by looping through the array */ } echo "</select>";

  • I’m sorry I was wrong. the goal is basically to dropdown a list with the information of a table and then go to the other table for the records that already have more than one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.