如何将嵌套的 foreach 值传递给 CanvasJS 图表?
Posted
技术标签:
【中文标题】如何将嵌套的 foreach 值传递给 CanvasJS 图表?【英文标题】:How to pass nested foreach Values to CanvasJS chart? 【发布时间】:2016-10-05 18:22:09 【问题描述】:我用下面的代码做了一些计算,然后我想将值传递给图表,但我无法发送它们我尝试了这个:
$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();`
【问题讨论】:
Passing php array as variables to javascript load url and back to php array?的可能重复 【参考方案1】:首先你需要把你的javascript代码放在画布图表之前,它会呈现。
如何将数据传递给视图很重要。你是作为 json 对象还是数组传递?
如果是 json 则需要先在 javscript 中解析,然后再以 php 编码发送。
在数据点参数中你可以这样传递
dataPoints: JSON.parse(<?php echo json_encode($json_managers) ?>)
或者可以在发送前通过 PHP 进行(示例代码 sn-p)
$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/
【讨论】:
你能检查一下我与canvasjs相关的新question吗?以上是关于如何将嵌套的 foreach 值传递给 CanvasJS 图表?的主要内容,如果未能解决你的问题,请参考以下文章
React useContext 不会将值传递给深层嵌套的子级
如何在 iOS Objective C 的嵌套 UICollectionViews 中从 Child UICollectionViewCell 的 UIButton 将值传递给新的 UIViewCon