The following code works with the CSV-file.
But I want to create more than one table and the values are in the same CSV.
How should I use the $rowcount in the second table, that should look like the first one, only with other first-value for th and the following values for the td's.
<a name="akademische-berufe"></a>
<table class="table-vocabulary">
<tbody>
<?php
$file = new SplFileObject("arbeit.csv");
$file->setFlags(SplFileObject::READ_CSV);
$rowcount = 0;
foreach ($file as $row) {
$rowcount++;
if ($rowcount == 1) {
list($vokabel_en, $vokabel_de) = $row;
printf('
<tr>
<th><img src="/bilder/symbole/play.png" data-text="%1$s" data-lang="'.$lang_code1.'" class="trigger_play"> %1$s</th>
<th><img src="/bilder/symbole/play.png" data-text="%2$s" data-lang="'.$lang_code2.'" class="trigger_play"> %2$s</th>
</tr>', $vokabel_en, $vokabel_de);
continue;
}
if ($rowcount >= 31) break;
list($vokabel_en, $vokabel_de) = $row;
printf('
<tr>
<td><img src="/bilder/symbole/play.png" data-text="%1$s" data-lang="'.$lang_code1.'" class="trigger_play"> %1$s</td>
<td><img src="/bilder/symbole/play.png" data-text="%2$s" data-lang="'.$lang_code2.'" class="trigger_play"> %2$s</td>
</tr>', $vokabel_en, $vokabel_de);
}
?>
</tbody>
</table>
Create a function with the parameters you want. Let the function also echo (print) the table tag and you are done.
User-defined php functions: http://www.php.net/manual/en/functions.user-defined.php
The code could then look like this:
<a name="akademische-berufe"></a>
<?php
function CreateLangTable($csvFile, $startRow, $endRow) {
if ($endRow < $startRow) {
return;
}
echo '<table class="table-vocabulary"><tbody>';
$csvFile->seek($startRow);
vprintf('
<tr>
<th><img src="/bilder/symbole/play.png" data-text="%1$s" data-lang="'.$lang_code1.'" class="trigger_play"> %1$s</th>
<th><img src="/bilder/symbole/play.png" data-text="%2$s" data-lang="'.$lang_code2.'" class="trigger_play"> %2$s</th>
</tr>',
$csvFile->current());
while ($csvFile->key() <= $endRow) {
$csvFile->next();
vprintf('
<tr>
<td><img src="/bilder/symbole/play.png" data-text="%1$s" data-lang="'.$lang_code1.'" class="trigger_play"> %1$s</td>
<td><img src="/bilder/symbole/play.png" data-text="%2$s" data-lang="'.$lang_code2.'" class="trigger_play"> %2$s</td>
</tr>',
$csvFile->current());
}
echo '</tbody></table>';
}
$file = new SplFileObject("arbeit.csv");
$file->setFlags(SplFileObject::READ_CSV);
CreateLangTable($file, 1, 31);
CreateLangTable($file, 33, 58);
?>
EDIT: In case you have empty rows dividing the tables then instead of
while ($csvFile->key() <= $endRow)
you can use
while (!empty($csvFile->current()))
you can then obviously omit/remove the function parameter $endRow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With