简体   繁体   English

php代码以数据透视表格式显示数据

[英]php code to display data in pivot table format

My Database has data like below.. 我的数据库具有如下数据。

surveyorname       bookingdate       receiptno
  raj              20-03-2015         56   
  raj              20-03-2015         701 
  angel            21-03-2015         55    
  raj              22-03-2015         700

Now My Code display data like below format.. 现在,我的代码将显示如下格式的数据。

surveyorname         20-03-2015
   raj                  2
  21-03-2015            --
   angel                1
  22-03-2015            --
   raj                  1

I need to display Report format like below... 我需要显示如下的报告格式...

 surveyorname    20-03-2015        21-03-2015        22-03-2015  
    raj             2                                    1
    angel                            1

My query getting data properly but only thing is I cannot understand how to display this in above format... 我的查询正确获取数据,但唯一的问题是我不明白如何以上述格式显示此信息...

   <?php
$book = $database->getRows("SELECT surveyor_name as srv, bookingdate as bd, COUNT(DISTINCT receipt_no) as num  FROM receipt_entry  GROUP BY surveyor_name, bookingdate");
$days=array();
$line=array();
foreach ($book as $b) {
   $days[$d['bd']]=1;
   if (!is_array($line[$d['srv']])) {
      $line[$d['srv']]=array();
   }
   $line[$d['srv']][$d['bd']]=$d['num'];
}
print "<table><tr><th></th>\n";
foreach ($days as $d=>$n) {
      print "<th>$d</th>\n";
}
print "</tr>\n";
foreach ($line as $srv=>$l) {
    print "<tr><td>$srv</td>";
    foreach ($days as $d=>$n) {
        print "<td>" . $l[$d] . "</td>\n";
    }
    print "</tr>\n";
} 
print "</table>\n";
?>

可以试试这个

SELECT count(*) as TotalNum, surveyorname, bookingdate FROM your_table group by bookingdate,surveyorname;
$book = $database->getRows("SELECT surveyor_name as srv
   , booking_date as bd
   , COUNT(*) as num 
   FROM receipt_entry 
   GROUP BY surveyor_name, booking_date 
   ORDER BY booking_date");

$days=array();
$line=array();
foreach ($book as $b) {
   $days[$d['bd']]=1;
   if (!is_array($line[$d['srv']])) {
      $line[$d['srv']]=array();
   }
   $line[$d['srv']][$d['bd']]=$d['num'];
}
print "<table><tr><th></th>\n";
foreach ($days as $d=>$n) {
      print "<th>$d</th>\n";
}
print "</tr>\n";
foreach ($line as $srv=>$l) {
    print "<tr><td>$srv</td>";
    foreach ($days as $d=>$n) {
        print "<td>" . $l[$d] . "</td>\n";
    }
    print "</tr>\n";
} 
print "</table>\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM