当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Transfering value from php array to jquery

问题描述:

Array $data2 is used for making charts,it contains product names of my company.i want to assign the values in $data2 i.e product names to jquery array 'categories' instead of places listed in the code below...??????

<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>

网友答案:
<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>
网友答案:

json_encode($data2) outputs valid code for an array/map in JS

 categories: <?= json_encode($data2) ?>,
网友答案:

Try this..

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

});

分享给朋友:
您可能感兴趣的文章:
随机阅读: