douningqiu4991 2014-01-21 16:47 采纳率: 0%
浏览 35

谷歌JavaScript表

Is possible to make this script take MySQL data? Can anybody show me?

This script is from google chars, table ... I try to make this script take MySQL data and display in a table.

<html>
  <head>
    <script type='text/javascript' src='https://www.google.com/jsapi'></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {packages:['table']});
      google.setOnLoadCallback(drawTable);
      function drawTable() {
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('number', 'Salary');
        data.addColumn('boolean', 'Full Time Employee');
        data.addRows([
          ['Mike',  {v: 10000, f: '$10,000'}, true],
          ['Jim',   {v:8000,   f: '$8,000'},  false],
          ['Alice', {v: 12500, f: '$12,500'}, true],
          ['Bob',   {v: 7000,  f: '$7,000'},  true]
        ]);

        var table = new google.visualization.Table(document.getElementById('table_div'));
        table.draw(data, {showRowNumber: true});
      }
    </script>
  </head>

  <body>
    <div id='table_div'></div>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongyuan9892 2014-01-21 16:53
    关注

    just fill your php array with values like this

    $array = array(
        array(
            array('01', array('v' => 10000, 'f' => '$10,000'), 'Mike'),
            ...
            array('12', array('v' => 12500, 'f' => '$12,200'), 'Mike'),
        )
    );
    

    then in your HTML code write :

    /* code starts here ... */
    function drawTable() {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'ID');
            data.addColumn('number', 'Salariu');
            data.addColumn('string', 'Name');
            data.addRows(<?php echo json_encode($array) ?>);
    
            var table = new google.visualization.Table(document.getElementById('table_div'));
            table.draw(data, {showRowNumber: true});
          }
    
    /* rest of code */
    

    UPDATE

    You have updated your source code and the addRows method argument format has changed, This how you should fill your php array (Drop the global array);

    $array = array(
        array('01', array('v' => 10000, 'f' => '$10,000'), 'Mike'),
        ...
        array('12', array('v' => 12500, 'f' => '$12,200'), 'Mike'),
    );
    

    UPDATE 2 Help with mysql data retrieving

    $array = array();
    $result = mysql_query('SELECT f1, f2, f3, f4 FROM some_table');
    while($row = mysql_fetch_assoc($result)) {
        $array[] = array($row['f1'], array('v' => $row['f2'], 'f' => $row['f3']), $row['f4']);
    }
    

    //Your array is now filled

    Hope it helps

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c