我试图想出一种有效的方式来显示一些数据,但根本没有成功。
该表是从几个表动态构建的,以显示一行标题,其中填充的列按站点显示结果,例如:
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
tblriskcategories risksecid, risksection
tblself selfauditid,siteid,auditid
tblsite siteid,sitename
所以 tblself 保存我们需要数据的站点列表和相关的审计 ID,
tblresultsnew 保存结果 - 总列 - 对于每个风险分区和每个审计 ID,例如,一个审计 ID 可以有大约 8 个风险分区,每个都有相应的总
tblriskcategories 保存列标题
tblsite 保存站点数据以使其有意义,
我希望这能进一步解释这个问题。
再次感谢所有帮助。
戴夫