2

I have a csv file that is converted to an html table by php (with help from Joby Joseph here): Dynamically display a CSV file as an HTML table on a web page

However, I would like to put the data into separate tables and container divs based on the values in the first two columns. So if this is my csv data:

League,Team,Date,Opponent,Result
american,MIN,May 3,UTA,W
american,MIN,May 4,SEA,L
american,SAC,May 3,DAL,L
american,SAC,May 4,TOR,W
national,NYN,May 5,BAL,L
national,NYN,May 7,MIA,W
national,DET,May 6,DEN,L
national,DET,May 7,POR,L

There would be four tables created (MIN, SAC, NYN, and DET), and they would be put into container divs that I have already created, "american-container" for the first two and "national-container" for the second two. Ideally, each table would have two header rows--the team name and the column labels--and the date/opponent/result data. Following is Joby's solution that puts the csv into one table:

function jj_readcsv($filename, $header=false) {
$handle = fopen($filename, "r");
echo '<table>';
//display header row if true
if ($header) {
    $csvcontents = fgetcsv($handle);
    echo '<tr>';
    foreach ($csvcontents as $headercolumn) {
        echo "<th>$headercolumn</th>";
    }
    echo '</tr>';
}
// displaying contents
while ($csvcontents = fgetcsv($handle)) {
    echo '<tr>';
    foreach ($csvcontents as $column) {
        echo "<td>$column</td>";
    }
    echo '</tr>';
}
echo '</table>';
fclose($handle);
}
jj_readcsv('test.csv',true);

Thanks for any help.

2
  • don't forget to mark my answer as solved if the problem is solved sir :) Commented May 20, 2016 at 6:31
  • Of course, but I'm still trying to figure part of it out. Commented May 20, 2016 at 7:16

1 Answer 1

2

This should work :

<?php
$csv = "League,Team,Date,Opponent,Result\namerican,MIN,May 3,UTA,W\namerican,MIN,May 4,SEA,L\namerican,SAC,May 3,DAL,L\namerican,SAC,May 4,TOR,W\nnational,NYN,May 5,BAL,L\nnational,NYN,May 7,MIA,W\nnational,DET,May 6,DEN,L\nnational,DET,May 7,POR,L";
$csv_array = explode("\n", $csv);
$tables = [];
foreach($csv_array as $key => $value) {
    if ($key == 0) {
        continue;
    }
    $line = explode(',', $value);
    if (array_key_exists($line[1], $tables)) {
        $tables[$line[1]][] = $line;
    } else {
        $tables[$line[1]] = [$line];
    }
}

foreach ($tables as $key => $value) {
    echo '<h1> ' .$key. ' </h1>'; // YOUR TITLE (Team)
    echo "<table>";
    echo '<tr>';
    foreach (explode(',', $csv_array[0]) as $keyHeader => $valueHeader) {
        if (in_array($keyHeader, [0, 1])) {
            continue;
        }
        echo "<th>$valueHeader</th>";
    }
    echo '</tr>';
    foreach ($value as $keyRow => $valueRow) {
        echo '<tr>';
            foreach ($valueRow as $keyValue => $valueValue) {
                if (in_array($keyValue, [0, 1])) {
                    continue;
                }
                echo "<td>$valueValue</td>";
            }
        echo '</tr>';
    }
    echo '</table>';
}

Here is what i get : Jsfiddle

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

3 Comments

Thanks a lot, Sofiene, but the first two columns shouldn't be part of the table data. The first column is for saying what div to put it in, while the second is a header row above the column labels, like so: NYN<br />Date Opponent Result<br>May 5 BAL L
@Beau i updated the code ;), i put the league in an H1 above the table :)
There's one other thing I can't figure out, Sofiene: how do you put the first column (american or national) into a table class (I have a jquery funtion to then append it to its matching div)? I know the info is in $tables[$line[0]], but I don't know how to get it into the table tag. Thanks.

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.