我正在尝试使用 php 和 mysql 创建一个 html 表来跟踪在多阶段过程中达到某些阶段的项目数。我需要将阶段的名称显示为倾斜的标题行(已使用 css3 对其进行排序,空白列作为第一行),并且每一行将在第一个单元格中具有项目类型的名称,然后是项目数相应列中的每个阶段。
这些阶段有一个数值,所以一个简单的数组就可以了——但是有 16 个可能的阶段,这必须是查询驱动的,因为不同的人会看到不同的阶段。
项目的类型根据另一种关系而有所不同(目前从 1 到 614 种类型),但我有一个查询告诉我要在每一行上显示的不同名称和项目类型的代码。
我的代码生成一个包含 16 个阶段的水平标题(在开始时加上一个额外的单元格以允许垂直图例)
数据行以图例单元格开始,但不知何故,循环给了我每行 32 个单元格(请帮助)。我期望的结果在第 1、第 3 等中。没有任何代码应该提供额外的空白,因为我输入了一个零表示不匹配。所以这里是:
// a previous $sql query checks for the distinct types of item in the batch in the table with joins to two other tables that accord with the two components of the CategoryCode
$today=date('H:i:s d-m-Y');
$categs=array();
while($row=mysql_fetch_assoc($result)){
$categs[$row['CategoryCode']]=$row['description'];
}
$headrow='';// sets variable for the header row
ed to
$bodyrow='';// sets variable for the body rows
$sql="SELECT stage_id, short_stage_desc FROM process_stages WHERE stage_omit='0' ORDER By showord"; // for various historic reasons the stages do not follow a 1,2,3 pathway so this query needs a showord column to produce some logic
$result = mysql_query($sql);
$cou=mysql_num_rows($result);
$stages=array(); // sets up array to check against
$s='1';
while($row=mysql_fetch_assoc($result)){
$s++; $ccolor = ($s % 2) ? "odd" : "even"; //for css background cell colour and the mix of div and spans that follow are part of doing 45 degree slanted column headings
$stageid=$row['stage_id']; $stagedesc=$row['short_stage_desc'];
$headrow.="<th class='skew'><div class='$ccolor'><span>$stagedesc</span></div></th>";
$stages[]=$row['stage_id']; //puts stages in display order
}
$headrow.="</tr></thead><tbody>";
//Now for the table bodyy
foreach($categs AS $key=>$category_descript){
$bodyrow.="<tr><td class='item-cat'>$category_descript</td>";//produces first cell as label
$sql="SELECT process_stage, COUNT(DISTINCT RowID) AS itemcount FROM item_detail WHERE CategoryCode='$key' GROUP BY process_stage";
$result=mysql_query($sql);
$gothro=array(); //will hold the key for the stage and number of items at that stage
while($row=mysql_fetch_assoc($result)){
$stag=$row['process_stage'];
$gothro[$stag]=$row['itemcount']; // fills array
}
$s='1';
reset($stages); // just ensure the array pointer is at start
foreach($stages AS $stagval){
$s++; $ccolor = ($s % 2) ? "odd" : "even"; // css for body row
if(array_key_exists($stagval,$gothro)){$bodyrow.="<td class='$ccolor'>".$gothro[$stagval]."<td>";}else{$bodyrow.="<td class='$ccolor'>0<td>";}
}
$bodyrow.='</tr>';
}
$outs="<br /><br /><div class='item-table-container'>
<h4>$bnam Situation at $today</h4>
<div class='item-table'>
<table><caption>$batch_description</caption><thead><tr><th>Item Type</th>$headrow.$bodyrow<tbody></table> </div></div> ";