سؤال

I have a mysql table like this

product | date | price

mobile | jan-14| 150 laptop | jan-14| 480 mobile | feb-14| 172

laptop | feb-14| 389

By using below code i got an output like this

product |jan 14 |feb 14|

mobile | 150 | 172 | laptop | 480 | 389 |

My PHP code(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();

?>

But,I need an output along with total of product wise and date wise like below,

product |jan 14 |feb 14|Prod total

mobile | 150 | 172 | 322

laptop | 480 | 389 | 869

date total| 630 | 561 |

could anyone help me? Thanks advance.

هل كانت مفيدة؟

المحلول

Would be like this:

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++;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top