2015-09-20 87 views
0

此代码读取用于创建表格的CSV文件。它工作正常。PHP格式表格标题

如何使第一行以标题样式(从表格CSS)格式化?

<table id="myTable" class="tablesorter animated fadeInDown"> <!-- cellspacing='0' is important, must stay --> 

<tbody> 
    <?php 
     $lines = file('graphdata/IndicForTableVsOthers.csv'); 


     foreach ($lines as $lineNum => $line) { 
    if($lineNum == 0) { 
    print "  <tr id=\"tr" . $lineNum . "\">"; 
    } 
     print "  <tr id=\"tr" . $lineNum . "\">"; 

     $tokens = str_getcsv($line);   

     print "<td style=\"width: 300px;\">" . trim($tokens[0]) . "</td>";  
     print "<td style=\"width: 100px;\">" . trim($tokens[1]) . "</td>"; 
     print "<td style=\"width: 100px;\">" . trim($tokens[2]) . "</td>"; 
     print "<td style=\"width: 100px;\">" . trim($tokens[3]) . "</td>"; 
     print "<td style=\"width: 100px;\">" . trim($tokens[4]) . "</td>"; 


     print "</script>\n"; 

     } 
    ?> 

    </tbody> 

+0

您是否在CSV文件中有表格标题?你的意思是,你想成为CSV的第一行成为列名? –

回答

0

这应该工作,我也改变了一些我认为会产生错误的marup其他部分。

<table id="myTable" class="tablesorter animated fadeInDown"> 

    <?php 
    $lines = file('graphdata/IndicForTableVsOthers.csv'); 

    foreach ($lines as $lineNum => $line) { 
     $cellType = ($lineNum == 0 ? "th" : "td"); 
     $tokens = str_getcsv($line); 

     if ($lineNum == 0) echo "<thead>"; 
     if ($lineNum == 1) echo "<tbody>"; 

     echo "<tr id=\"tr" . $lineNum . "\">"; 

     echo "<" . $cellType . " style=\"width: 300px;\">" . trim($tokens[0]) . "</" . $cellType . ">"; 
     echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[1]) . "</" . $cellType . ">"; 
     echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[2]) . "</" . $cellType . ">"; 
     echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[3]) . "</" . $cellType . ">"; 
     echo "<" . $cellType . " style=\"width: 100px;\">" . trim($tokens[4]) . "</" . $cellType . ">"; 

     echo "</tr>"; 

     if ($lineNum == 0) echo "</thead>"; 
    } 

    if (count($lines) > 1) echo "</tbody>"; 
    ?> 

</table> 
+1

我正在考虑类似的事情,但让我们注意到他打印的内容都是标签,而标签应该在 –

+0

谢谢@JuanBonnett,我纠正了它,还发现了一些其他错误(例如关闭小区的标签)。我没有测试代码,让我知道它是否还没有为你工作。 –

+0

现在有更多的意义,我不明白为什么它不应该工作 –

0

如果第一行印,但它看起来轻,你希望它大胆而漂亮,使用户知道它的头,添加一个类的便利。

table.cs-contents tr:first-child td { 
    font-weight: bold; 
    text-align: center; 
    border-bottom: 2px solid #eaeaea; 
}