duan117890 2017-04-09 16:40
浏览 34
已采纳

PieChart只显示一个元素

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!

  • 写回答

1条回答 默认 最新

  • duanfen7676 2017-04-09 20:27
    关注

    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);
    
            }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类