php从两个数组创建html表,mysql查询生成额外的单元格

时间:2013-10-13 09:49:21

标签: php mysql arrays

我正在尝试使用php和mysql创建一个html表来跟踪在多阶段过程中达到某些阶段的项目数。我需要将阶段的名称显示为倾斜的标题行(已经使用css3排序,空白列作为第一行),每行将具有第一个单元格中项目类型的名称,后面是项目的计数适当栏目中的每个阶段。

这些阶段有一个数值,所以一个简单的数组很好 - 但有16个可能的阶段,这必须是查询驱动的,因为不同的人会看到不同的阶段。

项目类型根据另一种关系(此时从一种类型到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> ";

1 个答案:

答案 0 :(得分:1)

看起来你错过了收尾td标记中的正斜杠:

... else{$bodyrow.="<td class='$ccolor'>0<td>";}