2016-08-30 51 views
0

我已经构建了一个很酷的脚本,它可以打开CSV并将数据输出到HTML表格中 - 整洁! :-)打破PHP循环并更改HTML

但是,我想知道,是否有可能采取第一行数据(表标题),并把它们放在theadth元素内?

<?php 
    echo "<table class='table table-bordered'>\n\n"; 
    $f = fopen("users.csv", "r"); 
    while (($line = fgetcsv($f)) !== false) { 
     $row = "<tr>"; 
     $is_empty = false; 
     foreach ($line as $cell) { 
      if ($cell !== '') { 
       $row .= "<td>" . htmlspecialchars($cell) . "</td>"; 
      } else { 
       $is_empty = true; 
      } 
     } 
     $row .= "</tr>\n"; 
     if ($is_empty) { 
      continue; 
     } else { 
      echo $row; 
     } 
    } 
    fclose($f); 
    echo "\n</table>"; 
?> 

现在我的HTML是:

<table class='table table-bordered'> 
<tr><td>Forename</td><td>Surname</td><td>Extension</td></tr> 
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr> 
</table> 

我可以将其更改为:

<table class='table table-bordered'> 
<thead><tr><th>Forename</th><th>Surname</th><th>Extension</th></tr></thead> 
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr> 
</table> 

谢谢你的任何指导。

回答

1

必须添加在你的代码

<?php 
    echo "<table class='table table-bordered'>\n\n"; 
    $f = fopen("users.csv", "r"); 

    $first_line=false; 

    while (($line = fgetcsv($f)) !== false) { 
     $row =""; 

     if($first_line == false) { 
      $row = "<thead><tr>"; 
      $col= "th"; 
     } 
     else { 
      $row = "<tr>"; 
      $col= "td"; 
     } 


     $is_empty = false; 

     foreach ($line as $cell) { 
      if ($cell !== '') { 
       $row .= "<".$col.">" . htmlspecialchars($cell) . "</".$col.">"; 
      } else { 
       $is_empty = true; 
      } 
     } 


     if($first_line == false) $row .= "</tr></thead>"; 
     else $row .= "</tr>"; 

     $first_line=true; 

     if ($is_empty) { 
      continue; 
     } else { 
      echo $row; 
     } 
    } 
    fclose($f); 
    echo "\n</table>"; 
?> 
+0

非常好! :-)当它位于'thead'内时,是否有可能用''代替'​​'? – michaelmcgurk

+1

修改......谢谢...... !!! –

+1

再次修改... –

0

您可以使用一个计数器来检测你显示的第一行:

echo "<table class='table table-bordered'>\n\n"; 
$f = fopen("users.csv", "r"); 
$counter = 0; 
while (($line = fgetcsv($f)) !== false) { 
    if ($counter == 0) { 
     $row .= "<thead><tr>"; 
     $is_empty = false; 
     foreach ($line as $cell) { 
      if ($cell !== '') { 
       $row .= "<th>" . htmlspecialchars($cell) . "</th>"; 
      } else { 
       $is_empty = true; 
      } 
     } 
     $row .= "</tr></thead>\n"; 
    } else { 
     $row .= "<tr>"; 
     $is_empty = false; 
     foreach ($line as $cell) { 
      if ($cell !== '') { 
       $row .= "<td>" . htmlspecialchars($cell) . "</td>"; 
      } else { 
       $is_empty = true; 
      } 
     } 
     $row .= "</tr>\n"; 
    } 

    $counter++; 
    if ($is_empty) { 
     continue; 
    } else { 
     echo $row; 
    } 
} 
fclose($f); 
echo "\n</table>"; 
0

是第一线的标识符。如果第一行始终是标题,则可以使用计数器作为行。

$rowCounter = 0; 
while (($line = fgetcsv($f)) !== false) { 
    $tr = '<tr>'; 
    $td = '<td>'; 
    $tr_end = '</tr>'; 
    $td_end = '</td>'; 
    if (++$rowCounter == 1){ 
     $tr = '<thead><tr>'; 
     $td = '<th>'; 
     $tr_end = '</tr></thead>'; 
     $td_end = '</th>'; 
    } 
... your code ... 
} 

现在你可以在你的代码中使用这些变量。可能有其他几种解决方案。

希望这会有所帮助!

0

如果使用函数分别生成标记文本,则会更加整齐。

function tr($data, $head = false, $xssProtect = true){ 
    // Formatting 
    $cellFmt = $head? "<th>%s</th>": "<td>%s</td>"; 
    $rowFmt = $head? "<thead><tr>%s</tr></thead>": "<tr>%s</tr>"; 
    $returnFmt = sprintf($rowFmt, str_repeat($cellFmt, count($data))); 

    // XSS 
    if ($xssProtect) $data = array_map('htmlspecialchars', $data); 

    // Format and return output 
    return vsprintf($returnFmt, $data); 
} 


echo "<table class='table table-bordered'>\n\n"; 
$f = fopen("users.csv", "r"); 
$line_no = 0; 
while (($line = fgetcsv($f)) !== false) { 
    echo tr($line, !$line_no); 
    $line_no++; 
} 
fclose($f); 
echo "\n</table>";