2016-08-30 58 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>";