用php交叉制表查询

时间:2012-07-16 18:33:02

标签: php mysql

我正在努力提出一种显示某些数据的有效方法,但根本没有取得成功。

该表是从几个表动态构建的,以显示一行标题,其中填充的列显示按站点显示的结果,例如:

Site name | Column A | Column B | Column C => column headers continue from DB query
 Site A   | Result A | Result B | Result C
 Site B   | Result C | Result B | Result C
 Site C   | Result C | Result B | Result C

Results keep going vertically.

这是我正在使用的查询

$risk_section=$row_risk_category['risksectid'];
    mysql_select_db($database_auditing, $auditing);
$qry_selfsites = sprintf("SELECT
tblself.siteid AS selfsite, 
tblsite.sitename AS sitename,
tblsite.address AS address, 
 tblpct.pctname AS pctname,
  tblresultsnew.total,
   tblresultsnew.auditid AS auditid,
    tblriskcategories.risksectid AS risksectid,
     tblriskcategories.risksection AS risksection
FROM tblself 
 LEFT JOIN tblresultsnew ON tblself.auditid = tblresultsnew.auditid 
  LEFT JOIN tblsite ON tblself.siteid = tblsite.siteid 
   LEFT JOIN tblpct ON tblsite.pctid = tblpct.pctid
    LEFT JOIN tblriskcategories ON
tblresultsnew.risksectid=tblriskcategories.risksectid 
WHERE tblsite.pctid IN (SELECT pctid FROM tblreportpcts WHERE
pctreportid='$pctreportid') 
 AND tblsite.sitetypeid IN (SELECT sitetypeid FROM tblreportsites WHERE
pctreportid='$pctreportid')
  AND tblself.year = %s 
   ORDER BY tblsite.pctid,tblsite.sitename", 
 GetSQLValueString($yearofrpt, "int"));
$selfsites = mysql_query($qry_selfsites, $auditing) or die(mysql_error());
$totalRows_selfsites = mysql_num_rows($selfsites);

所以问题是,有没有办法从上面的查询(或其改编版本)获取数据动态构建表,所有结果排列正确?  到目前为止,我只能让它垂直构建。

抱歉,编辑时间(使用下面的答案,我意识到我的问题措辞不是很好)

我想要获得的是查询中的一行列标题(tblriskcategories.risksection AS risksection)这些列水平填充以给列名称。

然后在下面显示网站名称,其结果对应于上面的列标题,即

<table>
<tr>
<th>Sitename</th>
<?php while($row_selfsites=mysql_fetch_assoc($selfsites){  
 //loop through the section headers pulled from the DB (tblriskcategories)  
<th><?php echo $row_selfsites['risksection'];//show the section headers?></th>
<?php }   
//end header loop then start another loop to show a row for each site pulled
 //out by the query and show the relevant results in the correct column  
while($row_selfsites=mysql_fetch_assoc($selfsites)) {  
 //do the vertical drop matching the header rows with the sitenames from tblsite 
 //and the results from tblresultsnew 
?>  
<tr>  
<td><?php echo $row_selfsites['sitename'];?></td>  
<td><?php echo $row_selfsites['total'];  
//these need to grow to fit the headers and each site?></td>  
<tr>  
<?php } //end displayed data loop?>  
</table>  

以下相关表格结构:
    tblresultsnew resultsid,auditid,risksectid,total
    风险分类风险分类     tblself selfauditid,siteid,auditid
    tblsite siteid,sitename

所以tblself保存我们需要数据的站点列表和相关的auditid,
tblresultsnew保存每个risksectid和每个auditid的结果 - 总列数,例如,一个auditid可以有大约8个risksectid,每个具有相应的总数
tblriskcategories包含列标题
tblsite保存站点数据,使其具有某种意义   我希望这能进一步解释这个问题。

再次感谢所有帮助。
戴夫

1 个答案:

答案 0 :(得分:0)

$rows = array(
0 => array('headingA' => 'results1a', 'headingB' => 'results1b',  ),
1 => array('headingA' => 'results2a', 'headingB' => 'results2b',  ),
2 => array('headingA' => 'results3a', 'headingB' => 'results3b',  ),
);
// $rows is spoofing a db result set as an array
//var_dump( $rows );

$first = true ;
$header_row = "START TABLE <br />" ;
$data_rows = "";
foreach( $rows as $row ) {
  foreach( $row as $key=>$value ){
    if( $first === true ){
    $header_row .= "$key  | ";
    }
  $data_rows .= "$value |";
  }
if( $first ) $header_row .="<br />";

$data_rows .= "<br />";
$first = false;
}
echo $header_row . $data_rows . "END TABLE";

给出:

START TABLE 
headingA | headingB | 
results1a |results1b |
results2a |results2b |
results3a |results3b |
END TABLE

这是你想要的那种解决方案吗?