php - 使用 PEAR 从数据库生成 Excel

标签 php mysql excel pear

我有一个像这样的 mysql 表

产品 |日期 |价格

手机 | 1 月 14 日| 150 笔记本电脑| 1 月 14 日|第480章 移动 | 2 月 14 日| 172

笔记本电脑 | 2 月 14 日| 389

通过使用下面的代码,我得到了这样的输出

产品 |1 月 14 日 |2 月 14 日|

手机 | 150 | 150 172 | 172 笔记本电脑| 480 | 480第389章

我的 PHP 代码(PEAR)

<?php writer, query fetching, worksheet,workbook, etc..on here
$product=array();
$date1=array();
$price=array();
while($rr=mysql_fetch_array($result))
{
$col=0;
$product[$rr['product']]=$rr['product'];
$date1[$rr['date']]=$rr['date'];
$price[$rr['prodcut']][$rr['date']]=$rr['price'];
}
$row=1;
$col=1;
foreach($date1 as $tkey=>$t)
{
$worksheet->write($row,$col++,$t);
}
$row=2;
foreach($product as $dkey=>$d)
{
$col=0;
$worksheet->write($row,$col++,$d);
foreach($date1 as $tkey=>$t)
{
$worksheet->write($row,$col++,$price[$d][$t]);
}
$row++;
}
$workbook->close();

?>

但是,我需要一个输出以及产品总数和日期,如下所示,

产品 |1 月 14 日 |2 月 14 日|产品总计

手机 | 150 | 150 172 | 172 322

笔记本电脑 | 480 | 480 389 | 389第869章

日期总计| 630 | 630 561 | 561

有人可以帮助我吗?提前致谢。

最佳答案

会是这样的:

foreach($product as $dkey=>$d)
{
   $col=0;
   $worksheet->write($row,$col++,$d);
   $total = 0;
   foreach($date1 as $tkey=>$t)
   {
      $worksheet->write($row,$col++,$price[$d][$t]);
      $total += $price[$d][$t];
   }
   $worksheet->write($row,$col++,$total);
   $row++;
}

关于php - 使用 PEAR 从数据库生成 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573145/

相关文章:

Excel VBA根据选择选择多个单元格

mysql - 在插入查询中添加 case 语句将空字符串转为 null

PHP memory_get_peak_usage 和 ini_set ('memory_limit' , '-1' )

mysql 当 UNION 显示 [Err] 1111 - 组函数的无效使用

python - 将 excel 或 csv 文件转换为 pandas 多级数据框

mysql - 是否可以像这样创建一个涉及来自两个表的行的查询?

php - MySQL PHP 在没有我输入的情况下添加 3306

php - preg_match 用于以 'for the' 开头的句子

javascript - CodeIgniter - 如何保存下拉所选值并在返回页面时设置它

php - PHP中的登录/注销问题