首页 > 解决方案 > 在回显表格时从数组中获取每 7 个元素

问题描述

我正在获取我需要的数据fgetcsv并将它们存储到$data. 它包含一个带有标题行和大量信息的表格。每 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";
    }
    $start++;
    echo '</tr>' . "\n";
}
fclose($handle);
echo '</table>';

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

标签: phpfgetcsv

解决方案


您检查每一列是第 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>';

推荐阅读