如何在PHPExcel中创建动态图表

时间:2016-07-01 17:18:26

标签: php mysql sql charts phpexcel

我在从SQL数据库中获取数据到PHPExcel中的动态图表时遇到问题

SQL代码

` $qry = "SELECT * FROM t_category 
  ORDER BY id_category";
      $sql = mysql_query($qry);
      while ($showcategory= mysql_fetch_array($sql)) {

            $id_category = $showcategory['id_category'];
            $name_category = $showcategory['name_category'];

            $qry2 = "SELECT SUM(stock) AS total_stok FROM t_product WHERE id_category ='$id_category'";

            $sql2= mysql_query($qry2);

            $datastock = mysql_fetch_array($sql2);  

            echo $showcategory['name_category']." :".$datastock ['total_stok']."<br/>"; 

      }` 

其中输出如下:

product1 : 20
product2 : 30
product3 : 10

等等......

问题是如何使这些SQL输出成为动态数组,这将在PHPExcel上实现,就像这样

$objWorksheet->fromArray(
 array(
    array('',   Product 1,  Product2,   Product3,  And So On...),
    array('Stock',   20,   30,  10, And So On...),

 )
);

1 个答案:

答案 0 :(得分:0)

试试吧:

$i = 0;
while ($showcategory= mysql_fetch_array($sql)) {
...   
    $objWorksheet->write($i, 0, $showcategory['name_category']);
    $objWorksheet->write($i, 1, $datastock ['total_stok']);
    ++$i;
}