我试图想出一种有效的方式来显示一些数据,但没有成功。使用php交叉制表查询
该表是动态地从几桌建,以显示与填充显示结果通过网站如列标题的行:
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,总
tblriskcategories risksectid,risksection
tblself selfauditid,SITEID,auditid
tblsite SITEID,站点名
因此,tblself拥有我们需要的数据和相关auditid的站点列表,
tblresultsnew保存结果 - 总列 - 每个risksectid和每个auditid,例如,一个auditid可以具有大约8个risksectid,每个具有相应的总数
tblriskcategories拥有列标题
tblsite拥有网站数据,使其意味着
我希望这可以进一步解释这个问题。
再次感谢您的帮助。
戴夫
您可以添加一个最终结果表应该是什么样子的例子,还是问题中提出的表格是什么样的,问题是“我如何使它看起来像这样”? – newfurniturey 2012-07-16 19:23:04
你能通过http://sqlfiddle.com/提供示例输入吗? – MvG 2012-07-16 19:47:52
嗨,原始问题中的桌子几乎看起来如何 - 我看到它的方式(至少在我的头上)是用两个while循环构建它 - 一个填充水平标题 – 2012-07-16 20:21:04