我在将数据从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...),

 )
);

最佳答案

试一试:

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

10-07 23:45