【问题标题】:Create Summary Table based on two column SQL query in PHP基于 PHP 中的两列 SQL 查询创建汇总表
【发布时间】:2014-11-08 18:53:44
【问题描述】:

我正在寻找一些帮助来构建一个通用的 PHP 函数,该函数将接收 SQL 查询的输出

SELECT DISTINCT categoryID, StatusID, COUNT( * ) FROM tableA GROUP BY categoryID

示例结果:

categoryID 状态ID COUNT( * ) 类别B 暂停 1 类别 A 保留 4 类别C 暂停 3 B 类草案 1

数据库中可以有任意数量的 CategoryID 和 Statuses...

并返回一个TABULAR表格格式:

我想要的结果是这样的:

 按类别分类的状态汇总表:
-------------------------------------------------- --------
          |甲类 | B类 |类别C | ... |全部的
已完成 | 0 | 1 | 0 | ... | 1
暂停 | 4 | 0 | 3 | ... | 7
草稿 | 0 | 1 | 1 | ... | 2
-------------------------------------------------- --------
总计:| 4 | 2 | 4 | ... | 10


【问题讨论】:

    标签: php sql report tabular


    【解决方案1】:

    我想通了! - 我希望这对将来的其他人有所帮助.. 它不是漂亮的代码,但它可以工作,而且它具有很强的遗传性,因此可以根据需要使用.. 叫谁:

    $sql="SELECT DISTINCT categoryID, statusID, COUNT( * ) as Count1 FROM entries GROUP BY categoryID" ; $results = mysql_query($sql, $con); // 现在我们有状态和类别,让我们将计数放在正确的单元格中: echo displayTabularSum ($myarray,"status","category",1) ;

    现在我使用的功能:

    函数 displayTabularSum($myarray, $table1,$table2,$includeTotals) { // 首先将$table1中的所有数据放入一个数组中 $sql = "SELECT * FROM $table1 WHERE 1"; $results=mysql_query($sql); $statusCodes= getsqlresultintoarray ($results) ; // 第二:将$table2中的所有数据放入一个数组中 $sql = "SELECT * FROM $table2 WHERE 1"; $results=mysql_query($sql); $categoryCodes= getsqlresultintoarray ($results) ; // 现在创建列中具有适当值的结果表 $statusTable=数组(); $out = ''; $第一=真; $cT=数组(); 对于 ($x=0; $x'; for ($y=0; $y'.$categoryCodes[$y][1].''; } if ($includeTotals) $out.= 'Total'; $out.= ''; $第一=假; } $out .=""; $out .="".$statusCodes[$x][1].""; $rT=0; 对于 ($y=0; $y"; $c1=searchForId($categoryCodes[$y][0], $myarray, "categoryID"); $c2=searchForId($statusCodes[$x][0], $myarray, "statusID"); $count1=0; $相同=($c1==$c2); 如果($相同){ 如果 ($c1==99999 或 $c2==99999) { // 什么都不做... 这些是 NULL } 别的 { $count1=$count1+$myarray[$c1]['Count1']; } } $out .= $count1; $rT=$count1+$rT; $cT[$y]=$cT[$y]+$count1; // 收集列总计 $out .=""; } if ($includeTotals) $out.= ''.$rT.''; // 显示行总数 $out .=""; } if ($includeTotals) { // 在关闭表之前显示列 Total。 $cT1=0; $out .=""; $out.= '总计:'; 对于 ($x=0; $x'.$cT[$x].''; $cT1=$cT1+$cT[$x]; } $out.= ''.$cT1.''; $out .=""; } $out .=""; 返回 $out; } 函数 getresultsintoarray1 ($results) { // 将 SQL 查询的所有结果放入数组中的函数。 $num_rows = mysql_num_rows($results); $myarray=array(); 如果 ($num_rows>0) { 而($row = mysql_fetch_assoc($results)) { $myarray[] = $row; } }别的 { echo "在数据库中没有找到数据..." ; 未设置($myarray); $myarray=array(); } 返回 $myarray; } function getsqlresultintoarray ($get) // 是 { $num_rows = mysql_num_rows($get); $returnArray=数组(); $i=0; 而($row1 = mysql_fetch_array($get)) { $returnArray[$i][0]=$row1[0]; // 这是身份证 $returnArray[$i][1]=$row1[1]; // 这是名字 $i++; } 返回 $returnArray; } 函数 searchForId($id, $array, $field) { 如果 (count($array)>0) { foreach ($array as $key => $val) { if ($val[$field] === $id) { 返回$键; } } } 返回 99999; }

    如果有人对如何改进有任何想法,我将不胜感激!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多