Skip to content
Advertisement

Getting every 7th element from array while echoing a table

I’m getting the data I need from fgetcsv and store them into $data. It contains a table with a header row and lot’s of info. Every 7th column is the path to where the file is stored.

I’ve already searched for what my problem is but I can’t seem the find the solution.

My Code yet:

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>';

I want to add a hyperlink via <a href=?> on every 7th column but I don’t know how. How can I do it and is that the right way?

Advertisement

Answer

You check every column is a 7th column or divisible by 7 you can just check if the variable is divided by 7 like this.

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>';
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement