Javascript中的循环不起作用
请帮忙 塞纳里奥: 我正在做一个PHP项目,它读取数据库中的数据,并使用图表或图形显示数据 问题: 但问题是,当我在Javascript中执行for循环时,它不起作用。帮帮我 以下是我的Javascript代码:Javascript中的循环不起作用,javascript,php,jquery,Javascript,Php,Jquery,请帮忙 塞纳里奥: 我正在做一个PHP项目,它读取数据库中的数据,并使用图表或图形显示数据 问题: 但问题是,当我在Javascript中执行for循环时,它不起作用。帮帮我 以下是我的Javascript代码: <script> $(function () { /* ChartJS * ------- * Here we will create a few charts using ChartJS */ //------------
<script>
$(function () {
/* ChartJS
* -------
* Here we will create a few charts using ChartJS
*/
//-------------
//- PIE CHART -
//-------------
// Get context with jQuery - using jQuery's .get() method.
var pieChartCanvas = $("#pieChart").get(0).getContext("2d");
var pieChart = new Chart(pieChartCanvas);
var PieData = [
{
<?php for($i=1;$i<$num_rows;$i++){ ?>
{
value: <?php echo $qtys[$i];?>,
color: "#d2d6de",
highlight: "#d2d6de",
label: "<?php echo $items[$i];?>"
}
<?php } ?>
];
pieChart.Doughnut(PieData, pieOptions);
});
</script>
$(函数(){
/*ChartJS
* -------
*这里我们将使用ChartJS创建一些图表
*/
//-------------
//-饼图-
//-------------
//使用jQuery获取上下文-使用jQuery的.Get()方法。
var pieChartCanvas=$(“#pieChart”).get(0.getContext(“2d”);
var pieChart=新图表(pieChartCanvas);
变量数据=[
{
{
值:,
颜色:“d2d6de”,
亮点:“d2d6de”,
标签:“
}
];
pieChart.甜甜圈(PieData,pieOptions);
});
这是我的PHP文件:
<?php
$num_rows = mysql_num_rows($result);
echo $num_rows;
$items=array();
$qtys=array();
while($row = mysql_fetch_array( $result)) {
$this->stock_id = $row["id_barang"];
$this->type = $row["jenis_barang"];
$items[]=$this->type;
$this->quantity = $row["kuantiti"];
$qtys[]=$this->quantity;
$this->weight = $row["berat"];
$this->category = $row["kategori"];
$this->supplier_id = $row["id_pembekal"];
}
?>
HTML文件:
<div class="col-md-6">
<!-- DONUT CHART -->
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Donut Chart</h3>
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
<button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="chart">
<canvas id="pieChart" style="height:250px"></canvas>
</div>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
油炸圈饼图
您似乎有语法错误:
var PieData = [
{ // <------------------------------------------
<?php for($i=1;$i<$num_rows;$i++){ ?>
{
value: <?php echo $qtys[$i];?>,
color: "#d2d6de",
highlight: "#d2d6de",
label: "<?php echo $items[$i];?>"
}
<?php } ?>
var-PieData=[
{ // ,
颜色:“d2d6de”,
亮点:“d2d6de”,
标签:“
}
正如莱昂所说,你有这个额外的{
在您的php循环之前,您需要删除,但还有一些其他内容。您需要在花括号中用逗号分隔项目。有很多方法可以做到这一点,但我通常使用的方法是将每个项目作为字符串放入数组中,然后对其进行内爆。这里我要做的是:
<?php
$data = array();
for($i=0;$i<$num_rows;$i++){ // (you need the element number 0 as well...)
$str = '
{
value: '.$qtys[$i].',
color: "#d2d6de",
highlight: "#d2d6de",
label: "'.$items[$i].'"
}';
$data[] = $str;
?>
var PieData = [
<?php
echo implode(",", $data);
?>
];
变量数据=[
];
您遇到了什么错误?欢迎使用stack overflow;)图形未显示。感谢@ww139谢谢您的帮助,但在我编写此代码后,图形未显示在我的页面中。我将其编码为与您向我显示的代码相同的代码。您可以上传您正在使用的文件的ZIP或发布小提琴,以便我可以尝试查看出了什么问题?