2016-08-30 2 views
0

Ich habe ein cooles Skript erstellt, das eine CSV öffnet und die Daten in eine HTML-Tabelle ausgibt - ordentlich! :-)PHP-Schleife brechen und HTML ändern

Allerdings habe ich mich gefragt, ist es möglich, die erste Reihe von Daten (Tabellenüberschriften) zu nehmen und diese innerhalb eines thead und th Elemente zu setzen?

<?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>"; 
?> 

Gerade jetzt mein HTML ist:

<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> 

Kann ich das ändern:

<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> 

Vielen Dank für jede Beratung.

Antwort

1

Sie erste Zeile Kennung in Ihrem Code

<?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

Ausgezeichnet! :-) Ist es möglich, '' durch '' zu ersetzen, wenn es in 'thead' ist? – michaelmcgurk

+1

Modifiziert ... danke ... !!! –

+1

wieder geändert ... –

0

können Sie einen Zähler verwenden, um feststellen, dass Sie die erste Zeile sind Anzeigen:

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

Ja hinzufügen müssen. Wenn die erste Zeile immer die Kopfzeile ist, können Sie einen Zähler für Zeilen verwenden.

$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 ... 
} 

Jetzt können Sie diese Variablen in Ihrem Code verwenden. Es könnte mehrere andere Lösungen geben.

Hoffe, das hilft!

0

Es wäre aufgeräumter, wenn Sie den Markup-Text separat mit einer Funktion erzeugen.

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>";