简体   繁体   中英

php create html table from two arrays and mysql query producing extra cells

I am trying to create an html table using php and mysql to track the number of items which have reached certain stages in a multi-stage process. I need to display the names of the stages as a slanted header row (already sorted that with css3, blank column as first) and each row will have the name of the type of item in the first cell, followed by the count of items at each stage in the appropriate columns.

The stages have a numerical value so a simple array is fine - but there are 16 possible stages and this has to be query-driven as different people will see different stages.

The types of item vary according to another relationship (anything from one to 614 types at this time) but I have a query that tells me the distinct name to display on each row and a code for the type of item.

My code produces a horizontal header with the sixteen stages (plus an extra cell at the start to allow for the vertical legend)

The rows of data start with the legend cell but somehow the loops are then giving me 32 cells per row (help please). The results I expect are in the 1st, 3rd etc. There is no code that should deliver an extra blank as I put a zero for no match. So here it is:

// 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> ";

看起来您在这里的td结束标记中错过了正斜杠:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM