PHP-在表列标题上插入一个Icon

时间:2015-12-25 09:37:03

标签: php csv

我正在使用此代码创建基于CSV文件的表格,并且工作正常;

<?php
                $lines = file('graphdata/GapsVsOthersForDimsOthersJson.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>";
  ?>

我有兴趣使用以下方法在最后一个列标题旁边插入一个按钮图标:

<button id="popupWindowDimensions" type="button" class="btn btn-default btn-sm">
      <span class="glyphicon glyphicon-info-sign"></span> 
</button>   

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下格式,但这会创建一个空的最后一列

所以请你能清楚地说明你需要的方式

 <?php
$lines = file('graphdata/GapsVsOthersForDimsOthersJson.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 . ">";
    if ($lineNum == 0)
    {
        echo "<" . $cellType . ' style=\"width: 100px;\">';
        echo '<button id="popupWindowDimensions" type="button" class="btn btn-default btn-sm">
        <span class="glyphicon glyphicon-info-sign"></span>
        </button>';
        echo '</' . $cellType . '>';
    }
    echo "</tr>";
    if ($lineNum == 0) echo "</thead>";
}
if (count($lines) > 1) echo "</tbody>";
?>