2015-09-04 20 views
0

我想要建立圖表,其中數據動態地來自數據庫。 到目前爲止,我可以通過創建存儲在數組中的虛擬數據來顯示此圖表。如何填充來自Doctrine FindAll()結果的值

控制器

//dummy data 
$data = array(
     array('OFW', 45.0), 
     array('Mindanao', 26.8), 
     array('Visayas', 12.8), 
     array('Luzon', 8.5), 
    ); 

現在我想以填補從database.I真實值的值正在使用Doctrine2這個項目

$island = $em->getRepository('DuterteBundle:Island')->findAll(); 
foreach($island as $is) { return $is } 
//where $is = island1, island2, island3 

然後我重構之前的虛擬數據像

$data = array(array($is, 67)); 

    return $this->render('Bundle:.........); 

此代碼將只顯示只有一個$值的圖形。在這種情況下,island1.顯示所有值的正確方法是什麼?

更新

我重構我的代碼..

// controller 
$island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i') 
     ->orderBy('i.name', 'ASC') 
     ->getQuery() 
     ->getResult(); 
    $island_names = array();//initialise array 
     foreach ($island as $is) { 
      $island_names[] = array(
       'name' => $is->getName() 
      ); 
     } 

    $val = array(); 
    foreach ($island_names as $key) { 
     $val = $key['name']; 
    } 
    $data = array(array($val,56.78)); 

傾銷數據

echo '<pre>'; 
    \Doctrine\Common\Util\Debug::dump($data); 
    echo '</pre>'; 

結果

array(1) { 
    [0]=> 
    array(2) { 
     [0]=> 
     string(7) "Visayas" 
     [1]=> 
    float(56.78) 
    } 
} 

更新 我重構我的代碼AGA在

$island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i') 
     ->orderBy('i.name', 'ASC') 
     ->getQuery() 
     ->getResult(); 
    $datas = array();//initialise array 
     foreach ($island as $is) { 
      $datas[] = array(
       'name' => $is->getName(), 
       'id' => 100 
      ); 
     } 

現在傾倒的數據將顯示所有預期的結果

array(4) { 
    [0]=> 
    array(2) { 
    ["name"]=> 
    string(5) "Luzon" 
     ["id"]=> 
     int(100) 
} 
    [1]=> 
    array(2) { 
    ["name"]=> 
    string(8) "Mindanao" 
    ["id"]=> 
     int(100) 
    } 
    [2]=> 
    array(2) { 
    ["name"]=> 
    string(3) "OFW" 
     ["id"]=> 
     int(100) 
} 
[3]=> 
    array(2) { 
    ["name"]=> 
     string(7) "Visayas" 
     ["id"]=> 
     int(100) 
    } 
} 

現在剩下的問題是填充到由圖表所需的陣列

僞數據看起來像這樣

$data = array(
     array('OFW', 45.0), 
     array('Mindanao', 26.8), 
     array('Visayas', 12.8), 
     array('Luzon', 8.5), 
    ); 

我試圖用這樣的實際值替換

$data = array(array($datas)); 

模板中的圖表不顯示。我可以看到傾倒的值。

更新

圖形控制器

public function graphAction(Request $request) 
{ 
    //we will count all voters here 
    $em = $this->getDoctrine()->getManager(); 
    $voters = $em->getRepository('DuterteBundle:Voters')->findAll(); 
    $voters_count = count($voters); 

    $ob = new Highchart(); 
    $ob->chart->renderTo('piechart'); 
    $ob->title->text("Island/OFW voter's shares"." ".(number_format($voters_count))."total votes as of " . date('F d,Y h:i A')."/Asia-Manila Time"); 
    $ob->plotOptions->pie(array(
     'allowPointSelect' => true, 
     'cursor' => 'pointer', 
     'dataLabels' => array('enabled' => false), 
     'showInLegend' => true 
    )); 

    $island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i') 
     ->orderBy('i.name', 'ASC') 
     ->getQuery() 
     ->getResult(); 
    $datas = array();//initialise array 
     foreach ($island as $is) { 
      $datas[] = array(
       'name' => $is->getName(), 
       'id' => 100 
      ); 
     } 
    echo '<pre>'; 
    \Doctrine\Common\Util\Debug::dump($datas); 
    echo '</pre>'; 
    $data = array(array($datas));//the problem is here 

    //with this dummy data, the chart/graph will work 

    /*$data = array(//how to replace this with real datas? 
     array('OFW', 45.0), 
     array('Mindanao', 26.8), 
     array('Visayas', 12.8), 
     array('Luzon', 8.5), 
    );*/ 
    $series = array(
     array('type' => 'pie','name' => "Voter's share", 'data' => $data) 
    ); 
    $ob->series($series); 
    return $this->render('GraphBundle:Default:graph.html.twig', array(
     'chart' => $ob, 
     'votercount' => $voters_count 
    )); 

} 
+0

以dB值存在呢?簡單地說:'$ data = array(); foreach($ island爲$ is){$ data [] = array($ is-> getName(),$ is-> getValue()); }' – malcolm

+0

我仍然在計算db.Island表中的值,它有名稱和id列。它與選民實體有關,島上有很多選民,選民有一個島。我想顯示的值是選民人數每個island.Maybe我實現了在加入查詢 –

+0

我不明白你的問題,你正在做'$ val = $ name ['key']'這種方式'$ val'將**總是**的名稱**最後**島。 – user2268997

回答

0

如果您有結果許多名稱和要算來,你可以使用array_count_values PHP函數內部全碼:

$data = array(); 
foreach ($island as $is) { 
    $data[] = $is->getName(); 
}; 

$data = array_count_values($data); 

和如果你想按照格式描述:

$results = array(); 
foreach ($data as $key => $value) { 
    $results[] = array($key, $value); 
} 
0

我明白了。我改變foreach循環

$datas = array();//initialise array 
     foreach ($island as $is) { 
      $datas[] = array(
       $is->getName(),100 
      ); 
     } 

用戶馬爾科姆的建議