繁体   English   中英

PHP中的动态标题和表格单元格

[英]Dynamic heading and table cells in php

我在做动态表时遇到问题

我的表结构如下:

======================================================================
tender_id | coc | datasheet | datecode | shelflife | Suppliername
======================================================================
   201    |  No |   Yes      |     Yes  |     No   |  Supplier1
   201    | Yes |   No       |     Yes  |     No   |  Supplier2
   201    | No  |   No       |     No   |     No   |  Supplier3

我在PHP中的预期结果是

=============================================
 COC | Datasheet | Date Code | Supplier Name
=============================================
   -     Yes          Yes       Supplier1
  Yes    -            Yes       Supplier2

结果应仅在值是“是”时显示。 如果所有值均为“否”,则甚至不显示标题。 例如,在上述条件下,所有供应商的保质期值为“否”。 在这种情况下,甚至标题也不会显示。 我已经尝试了一些脚本,但是存在一些问题。

这是我尝试的脚本:

$sql_shelf = "SELECT tender_id, suppliername, coc, date_code, shelf_life , datasheet FROM comparitive_statement1 WHERE (coc='Yes' OR technical_compliance='Yes' OR date_code='Yes' OR shelf_life='Yes' OR datasheet='Yes') tender_id='$tender_id' group by suppliername";

    $result = mysql_query($sql_shelf) or die($sql_shelf."<br/><br/>".mysql_error());
    $i = 0;
    while ($list = mysql_fetch_array($result)) {
    if($i == 0){
    echo '<tr>';
    $sh = $list['shelf_life'];
    if ($sh=="No") {
    } else {
    echo '<td><b>Shelf Life</b></td>';
    }
      echo '</tr>';
      $i++;
      }

    echo '<tr>';
    $sl1 = $list['shelf_life'];
    if ($sl1=="No") {
    } else {
    echo "<td>{$list['shelf_life']}</td>";\
    echo "</tr>";
    }
     ++$i;
     }

脚本有点长,所以我刚刚放入了与保质期有关的记录。

为了确定是否应显示整个列,您将需要首先遍历所有记录。 我还没有调试以下内容,但是希望如果它不起作用,它将足以让您了解需要执行的操作:

$result = mysql_query($sql_shelf) or die($sql_shelf."<br/><br/>".mysql_error());
$stuff = array(); //an array for all of the rows
$columns = array(); //an array for the columns
while ($list = mysql_fetch_array($result,MYSQL_ASSOC)) {
    $stuff[] = $list; //add list to the appropriate array
    if(empty($columns)) { foreach($list as $key=>$val) { $columns[$key] = 0; } } //fill $columns with a dynamic number of 0s based on how many columns the database returns
    foreach($list as $key=>$val) { if(!is_numeric($val) && $val!= 'No') { $columns[$key] = 1; } } //sets column array value to 1 for any non-numeric/non-no answer
}
//now we actually build the table
echo "<table><thead>";
echo "<tr>";
foreach($columns as $name=>$display){
    if($display== 1) { echo "<th>".$name."</th>"; } //if we set the value earlier to 1, then we want the column to show
}
echo "</tr></thead><tbody>";
foreach($stuff as $list){
    echo "<tr>";
        foreach($list as $key=>$val){
            if($columns[$key] == 1){ echo "<td>".$val."</td>";
        }
    echo "</tr>";
}
echo "</tbody></table>";

编辑:添加表格标签

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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