Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Foreach循环只显示画布图表中的最后一条记录,php json不能在javascript中使用_Javascript_Laravel - Fatal编程技术网

Foreach循环只显示画布图表中的最后一条记录,php json不能在javascript中使用

Foreach循环只显示画布图表中的最后一条记录,php json不能在javascript中使用,javascript,laravel,Javascript,Laravel,我正在使用画布显示销售报告,我想显示产品销售日期查询获取结果的总和,但由于forecah循环,我只获取了最后一条记录。我从控制器获取了四条记录,但在blade文件中,我只找到了最后一条记录 控制器: public function display_sales_report(Request $req) { $data=$req->all(); $get_details=DB::select('SELECT sum(orders_qty) as sum_

我正在使用画布显示销售报告,我想显示产品销售日期查询获取结果的总和,但由于forecah循环,我只获取了最后一条记录。我从控制器获取了四条记录,但在blade文件中,我只找到了最后一条记录

控制器:

public function display_sales_report(Request $req)
    {
        $data=$req->all();
        $get_details=DB::select('SELECT sum(orders_qty) as sum_of_qty,deliver_date FROM `orders` WHERE deliver_date between ? AND ? GROUP BY deliver_date',[$data['start_date'],$data['end_date']]);
        return view('dashboard',['get_data'=>$get_details]);
    }
刃锉 :


JavaScript:

<script>
window.onload = function() {

var chart = new CanvasJS.Chart("chartContainer", {
    animationEnabled: true,
    theme: "light2",
    title:{
        text: "Sales Report"
    },
    axisY: {
        title: "Number of product sold"
    },
    data: [{
        type: "column",
        yValueFormatString: "#,##0.## tonnes",
        dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
    }]
});
chart.render();

}
</script> 

window.onload=函数(){
var chart=new CanvasJS.chart(“chartContainer”{
animationEnabled:没错,
主题:“light2”,
标题:{
正文:“销售报告”
},
axisY:{
标题:“售出产品数量”
},
数据:[{
键入:“列”,
yValueFormatString:“#,###0.###吨”,
数据点:
}]
});
chart.render();
}

因为在每个循环中将数据设置为
$dataPoints
。您需要将每个数据放入每个循环中,如下所示:

foreach($get_data as$data)
{
$dataPoints[]=数组(“y”=>$data->数量之和,“标签”=>$data->交货日期);
}
在javascript代码中,您需要使用
JSON.parse()
php JSON字符串解析为javascript数组:

JSON.parse()

绝对!,因为您只是重新分配了datapoints变量的值,而不是在datapoints上推送新值。您的代码应该是这样的

foreach($get_data as $data)
{
    $dataPoints[] = array("label" => $dat->deliver_date, "y" => $data->sum_of_qty);
}

您好,我正在使用它,但它不能显示图形plz检查java脚本代码,我正在将每个数据传递给jsonencode@smitaranipatil尝试使用
JSON.parse()
您好,我正在使用它,但它无法显示图形plz检查java脚本代码我正在将每个数据的此代码传递给JSON encode为什么要将其转换为JSON\u encode?它将转换为字符串。只需使用JSON.parse($dataPoints);将您的dataPoints更改为此,dataPoints:JSON.parse({JSON_encode($dataPoints)}}”)(2/2)ErrorException htmlspecialchars()期望参数1是字符串,数组给定(视图:D:\xampp\htdocs\avitra\resources\views\dashboard.blade.php)我得到这个错误,dataPoints:{{$dataPoints}”?
foreach($get_data as $data)
{
    $dataPoints[] = array("label" => $dat->deliver_date, "y" => $data->sum_of_qty);
}