3

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>

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, but I don't know how? Can you show me it?
Ok, no problem. Give me few minutes.
How exactly would you like to change the values in th and td? What should change there?
first table th 1 td's 2-31, second table th 33, td's 34-58
Yes, well list is a pretty old construction that should not be used at all in php in my opinion. And since you use the SplFileObject, maybe it would be wise to do the whole thing in an object...but the function will do fine as well. There are some basic design mistakes in the code though, but you will learn that in time and practise.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.