回显表时从数组中获取第7个元素

问题描述 投票:1回答:2

我正在从fgetcsv中获取所需的数据并将其存储到$data中。它包含一个带有标题行和大量信息的表。每7个collum是文件存储的路径。

我已经搜索了问题所在,但似乎找不到解决方法。

尚未输入我的代码:

echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    echo '<tr>' . "\n";
    for ( $x = 0; $x < count($data); $x++) {
        if ($start == 0 && $hasTitle == true)
            echo '<th>'.$data[$x].'</th>' . "\n";
        else
            echo '<td>'.$data[$x].'</td>' . "\n";
    }
    $start++;
    echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';

我想在每7个collum上通过<a href=?>添加超链接,但我不知道如何。我该怎么做,这是正确的方法吗?

php fgetcsv
2个回答
0
投票

检查计数器的remainder乘7是否为0。如果为0,则为7的倍数,您可以回显所需的字符串。

if($start % 7 === 0){
  echo '<a href=?>'
}

0
投票

您检查每列是第7列还是被7整除,您可以像这样检查变量是否被7除。

echo '<table border="0" cellspacing="0" cellpadding="5" class="csvTable" width="auto">';
$handle = fopen("index.csv", "r");
$start = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    echo '<tr>' . "\n";
    for ( $x = 0; $x < count($data); $x++) {
        if ($start == 0 && $hasTitle == true)
            echo '<th>'.$data[$x].'</th>' . "\n";
        else
            echo '<td>'.$data[$x].'</td>' . "\n";
        if( $x && !($x % 7) ){
        echo '<a href=?>'
        }
    }

    $start++;
    echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';
© www.soinside.com 2019 - 2024. All rights reserved.