doujia9204 2015-09-30 22:51
浏览 30
已采纳

Google图表中的PHP字符串连接

I am passing PHP session variables to a Google chart object. The following works fine:

      function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Descriptor', 'Items checked/Total number of items', { role: 'annotation' }],
      ['Attention', <?php echo $_SESSION["A"];?>, <?php echo $_SESSION["A"];?>],
      ['Auditory', <?php echo $_SESSION["AUD"];?>, <?php echo $_SESSION["AUD"];?>]           
  ]);

Everything works fine when the $_SESSION["A"] variable is by itself, and will print an interger in the chart ie. 12. However, when I try to concatenate '/12' to the $_SESSION["A"] variable in hopes of echoing '12/12' in the chart, I end up getting '1' in the chart (it is doing the math on 12/12). Here is the faulty code:

      function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Descriptor', 'Items checked/Total number of items', { role: 'annotation' }],
      ['Attention', <?php echo $_SESSION["A"];?>, <?php echo $_SESSION["A"]."/12";?>],
      ['Auditory', <?php echo $_SESSION["AUD"];?>, <?php echo $_SESSION["AUD"]."/12";?>]           
  ]);

How do I concatenate "/12" to an integer without it doing the math?

  • 写回答

1条回答 默认 最新

  • duan198811 2015-09-30 22:59
    关注

    put single quotes so that js does not make the math and treat the value as a string instead:

    ['Auditory', <?php echo $_SESSION["AUD"];?>, '<?php echo $_SESSION["AUD"]."/12";?>']           
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存