如何将嵌套的foreach值传递给CanvasJS图表?

时间:2016-06-06 08:08:10

标签: javascript php html5 html5-canvas

我使用下面的代码进行一些计算,之后我想将值传递给图表,但我无法发送它们,我试试这个:

 $getStoreIDs = mysqli_query($dbConnection, "SELECT DISTINCT Stores_storeID FROM maintable WHERE Survey_surveyId = '$surveyID' ");

            while ($row = mysqli_fetch_array($getStoreIDs))
             { 
                $storeIDarray[] = $row;

             }

    foreach($_POST['sections'] as $selected)
            {

        foreach ($storeIDarray as $key => $row)
            {
                $storeID = $row['Stores_storeID'];



                      $storeNames= mysqli_query($dbConnection , "SELECT storeName FROM stores WHERE storeID = '".$row['Stores_storeID']."'");
                     $sectinName = mysqli_query($dbConnection , "SELECT sectionName FROM sections WHERE sectionID = '$selected' ");


                    $totalSections = mysqli_query($dbConnection, "SELECT SUM(itemPrice) AS totalSection FROM maintable WHERE items_Family_Sections_sectionID='$selected' AND Stores_storeID = '$storeID' AND Survey_surveyId = '$surveyID' ");


                } 
            }

我想传递($ storeNames,$ ectionName和$ totalSelections)的值 这是我的CanvasJs代码`

var chart1 = new CanvasJS.Chart("chartContainer1",
{
  title:{
    text: "Store Comparision"    
  },
  animationEnabled: true,
  axisY: {
    title: "Total Price"
  },
  legend: {
    verticalAlign: "bottom",
    horizontalAlign: "center"
  },
  theme: "theme2",
  data: [

  {        
    type: "column",  
    showInLegend: true, 
    legendMarkerColor: "grey",
    dataPoints: [  



    ]
  }   
  ]
});

chart1.render();`

1 个答案:

答案 0 :(得分:0)

首先,你需要将你的javascript代码放在画布图表之前,它会渲染。

如何将数据传递到视图非常重要。您是否以json对象或数组的形式传递?

如果json需要在发送编码的php之前解析javscript。

在datapoints paraemeter中你可以像这样传递

dataPoints: JSON.parse(<?php echo json_encode($json_managers) ?>)

或者可以在发送之前通过PHP(示例代码段)

$managers = DB::table('orders')
    ->join('managers', 'orders.manager_id', '=', 'managers.id')
    ->groupBy('managers.manager_name')
    ->get(['managers.manager_name',DB::raw("sum(orders.sale) as sale")]);

    foreach($managers as $key => $value){
        $point = array("label" => $value->manager_name , "y" => $value->sale);
        array_push($data_manager_points, $point);
    }
    $json_managers = json_encode($data_manager_points,JSON_NUMERIC_CHECK);

了解更多信息:

http://canvasjs.com/forums/topic/how-can-i-use-php-mysql-dynamic-data/