I have a table containing users, each user has a city. I am using ObHighchartsBundle to display the number of users in each city
this is my code
$em = $this->getDoctrine()->getManager();
$classes = $em->getRepository('MyBundle:User')->findAll();
$data= array();
$stat=array();
foreach($classes as $class) {
$query = $em->createQuery("SELECT COUNT(u) FROM MyBundle:User u WHERE u.city = ?1");
$query->setParameter(1, $class->getCity());
if (!in_array($class->getCity(), $stat)) {
array_push($stat,$class->getCity(),($query->getSingleScalarResult() *1)/1);
}
}
array_push($data,$stat);
I am only getting the number of users in the first city, so seems like my foreach
loop is not working well .. Any help would be appreciated!
This is my working code in case somebody needs it:
$em = $this->getDoctrine()->getManager();
$classes = $em->getRepository('MyBundle:User')->findAll();
$data= array();
$stat=array();
$hm=array();
$query = $em->createQuery("SELECT COUNT(u) FROM MyBundle:User u WHERE u.city = ?1");
foreach($classes as $class) {
$query->setParameter(1, $class->getCity());
if (!in_array($class->getCity(), $hm)) {
unset($stat);
$stat = array();
array_push($stat, $class->getCity(), (int)$query->getSingleScalarResult());
array_push($hm, $class->getCity());
array_push($data,$stat);
}
}