0

数组 $data2 用于制作图表,它包含我公司的产品名称。我想将 $data2 中的值(即产品名称)分配给 jquery 数组“类别”,而不是下面代码中列出的位置...?

<script type="text/javascript">
 $(function () {
 var chart;
 $(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column',
            margin: [ 50, 50, 100, 80]
        },
        title: {
            text: 'Product Selling Report'
        },


  <?php

  $sql="select productid,count(productid)as num from orderdetails group by productid order by num desc";
$res=mysql_query($sql);
$i=0;
   while($arr=mysql_fetch_array($res))
    {
    $rep=$arr['productid'];
    $sql2="select * from product where productid='$rep'";
    $res2=mysql_query($sql2);
    $arr2=mysql_fetch_array($res2);
    $data2[$i]=$arr2['productname'];
    $i=$i+1;
    }

     ?>
        xAxis: 
            categories: [
                'Tokyo',
                'Jakarta',
                'New York',
                'Seoul',
                'Manila',
                'Mumbai',
                'Sao Paulo',
                'Mexico City',
                'Buenos Aires',
                'Guangzhou',
                'Shenzhen',
                'Istanbul'
            ],
      },
      });
  });
   </script>
4

3 回答 3

1

json_encode($data2)为 JS 中的数组/映射输出有效代码

 categories: <?= json_encode($data2) ?>,
于 2012-04-13T06:18:54.543 回答
1
<script type="text/javascript">
 $(function () {
 var chart;
 $(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'column',
            margin: [ 50, 50, 100, 80]
        },
        title: {
            text: 'Product Selling Report'
        },
        xAxis: 
            categories: [

  <?php

  $sql="select productid,count(productid)as num from orderdetails group by productid order by num desc";
$res=mysql_query($sql);
$i=0;
   while($arr=mysql_fetch_array($res, MYSQL_ASSOC))
    {
    $rep=$arr['productid'];
    $sql2="select * from product where productid='$rep'";
    $res2=mysql_query($sql2);
    $arr2=mysql_fetch_array($res2, MYSQL_ASSOC);
    echo "'".$arr2['productname']."',\n";
    }

     ?>
            ],
      },
      });
  });
   </script>
于 2012-04-13T06:20:10.853 回答
0

试试这个..

xAxis: 
        categories: [
            <?php $str = "";
                  foreach($data2 as $data) {
                      $str .= $data.',';
                   }
                   $str = substr($str,0,-1);
                   echo $str;
        ],
  },
  });

});

于 2012-04-13T06:22:27.990 回答