PhpMygraph - 如何从数据库mysql中分配数组

时间:2013-08-14 08:33:25

标签: php mysql

这是我的代码:

 <?php    
    //Set content-type header
    header("Content-type: image/png");

    //Include phpMyGraph5.0.php
    include_once('phpMyGraph5.0.php');
    include("koneksi.php");

    //Set config directives
    $cfg['title'] = 'Example graph';
    $cfg['width'] = 500;
    $cfg['height'] = 250;


    //Set data
    $query="select col1, col2 from tab1 where sub_project = 'sometext'";
    $result=mysql_query($query);
    $row = mysql_fetch_assoc($result);
    $data = array(
        $row['col1'] => $row['col2'],
    );

    //Create phpMyGraph instance
    $graph = new phpMyGraph();

    //Parse
    $graph->parseVerticalColumnGraph($data, $cfg);
?>

在我的情况下,col1中的数据是文本,而col2中的数据是数字。 但是使用该代码,图表只显示第一行值。 同时数据库中的值不止一个值。

1 个答案:

答案 0 :(得分:0)

在while循环中添加获取的结果,如下所示。

<?php    
    //Set content-type header
    header("Content-type: image/png");

    //Include phpMyGraph5.0.php
    include_once('phpMyGraph5.0.php');
    include("koneksi.php");

    //Set config directives
    $cfg['title'] = 'Example graph';
    $cfg['width'] = 500;
    $cfg['height'] = 250;


    //Set data
    $query="select col1, col2 from tab1 where sub_project = 'sometext'";
    $result=mysql_query($query);
    $data = array();
    while($row = mysql_fetch_assoc($result))
    {
       $data[$row['col1']] = $row['col2'];
    }

    //Create phpMyGraph instance
    $graph = new phpMyGraph();

    //Parse
    $graph->parseVerticalColumnGraph($data, $cfg);
?>

并且$data是多行的,并且包含来自DB的所有记录

希望此代码可以帮助您