Search code examples
phphtmlcsvgenerated-code

Generate html code from csv file


I have one csv file separated by the following character |. The file has three columns; one is the url(COL1), the other is a small text(COL3), and the last one its an image location(COL2). I need a script that gives me the html code like this with the data from the csv file:

<a href="COL1"><IMG SRC="COL2" />COL3</a>

How can I do this?


Solution

  • You using a serverside language, I'm assuming? With php, you'd be after fgetcsv.
    Here's one example: How to import csv file in PHP?


    UPDATED:

    Try this:

    $separation_char = "|"; // character separating fields
    $filename = "test.csv";
    
    $row = 1;
    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, $separation_char)) !== FALSE) {
            echo '<a href="' . $data[0] . '"><IMG SRC="' . $data[1] . '" />' . $data[2] . '</a>';
        }
        fclose($handle);
    }