[英]Output from CSV import in PHP function
I have a function in PHP, which create a HTML table with data in CSV files. 我在PHP中有一个函数,该函数用CSV文件中的数据创建一个HTML表。
The problem is the title of each table is twice printed. 问题是每个表的标题被打印两次。 First in
<th>
and then in the first <tr>
. 首先在
<th>
,然后在第一个<tr>
。 I want the title only printed in the <th>
and not in the following <tr>
. 我希望标题仅显示在
<th>
,而不在以下<tr>
。 What is the mistake and do you have other recommendation for the PHP function. 这是什么错误,您对PHP函数有其他建议吗?
csv example CSV范例
title a, Titel A
example1, Beispiel1
example2, Beispiel2
title b, Titel B
example1, Beispiel1
example2, Beispiel2
php function PHP功能
function CreateLangTable($csvFile, $startRow, $endRow, $number) {
global $lang_code1, $lang_code2, $vocabulary_group, $urlArray;
if ($endRow < $startRow) {
return;
}
echo '
<a id="' . $vocabulary_group[$urlArray[4]][$number] . '"></a>
<div class="table-responsive">
<table class="table table-hover table-bordered table-striped table-fixed">
<thead class="blue-grey lighten-4">';
$csvFile->seek($startRow);
vprintf('
<tr>
<th><div data-text="%1$s" data-lang="' . $lang_code1 . '" class="trigger_play"> %1$s</div></th>
<th><div data-text="%2$s" data-lang="' . $lang_code2 . '" class="trigger_play"> %2$s</div></th>
</tr>
</thead>
<tbody>', $csvFile->current());
while ($csvFile->key() <= $endRow) {
vprintf('
<tr>
<td><div data-text="%1$s" data-lang="' . $lang_code1 . '" class="trigger_play"> %1$s</div></td>
<td><div data-text="%2$s" data-lang="' . $lang_code2 . '" class="trigger_play"> %2$s</div></td>
</tr>', $csvFile->current());
$csvFile->next();
}
echo '
</tbody>
</table>
</div>' . "\n";
}
if ($urlArray[3] == "vokabeln" && isset($urlArray[4])) { // Undefined offset: 3 [only until /url1/url2/]
$file = new SplFileObject($_SERVER['DOCUMENT_ROOT'] . "/$urlArray[1]/csv/$urlArray[4].csv");
$file->setFlags(SplFileObject::READ_CSV);
}
php code for generation the HTML tables for this CSV example: 用于生成此CSV示例的HTML表的php代码:
CreateLangTable($file, 0, 2, 0);
CreateLangTable($file, 4, 6, 1);
The $csvFile->seek($startRow);
$csvFile->seek($startRow);
line gets the pointer to the header. 行获取指向标题的指针。
Then the while ($csvFile->key() <= $endRow)
starts looping from the same point. 然后
while ($csvFile->key() <= $endRow)
从同一点开始循环。 There should be a $csvFile->next();
应该有一个
$csvFile->next();
before 'while()' 在“ while()”之前
That's 那是
$csvFile->next();
while ($csvFile->key() <= $endRow) {...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.