0

I have built a cool script which opens a CSV and outputs the data into an HTML table - neat! :-)

However, I was wondering, is it possible to take the first row of data (table headings) and put these inside a thead and th elements?

<?php
    echo "<table class='table table-bordered'>\n\n";
    $f = fopen("users.csv", "r");
    while (($line = fgetcsv($f)) !== false) {
        $row = "<tr>";
        $is_empty = false;
        foreach ($line as $cell) {
            if ($cell !== '') {
                $row .= "<td>" . htmlspecialchars($cell) . "</td>";
            } else {
                $is_empty = true;
            }
        }
        $row .= "</tr>\n";
        if ($is_empty) {
            continue;
        } else {
            echo $row;
        }
    }
    fclose($f);
    echo "\n</table>";
?>

Right now my HTML is:

<table class='table table-bordered'>
<tr><td>Forename</td><td>Surname</td><td>Extension</td></tr>
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr>
</table>

Can I change this to:

<table class='table table-bordered'>
<thead><tr><th>Forename</th><th>Surname</th><th>Extension</th></tr></thead>
<tr><td>Jim</td><td>Carey</td><td>9843</td></tr>
</table>

Thank you for any guidance.

4 Answers 4

1

you have to add first line identifier in your code

<?php
    echo "<table class='table table-bordered'>\n\n";
    $f = fopen("users.csv", "r");

    $first_line=false;

    while (($line = fgetcsv($f)) !== false) {
        $row ="";

        if($first_line == false) {
             $row = "<thead><tr>";
             $col= "th";
        }
        else {
             $row = "<tr>";
             $col= "td";
        }


        $is_empty = false;

        foreach ($line as $cell) {
            if ($cell !== '') {
                $row .= "<".$col.">" . htmlspecialchars($cell) . "</".$col.">";
            } else {
                $is_empty = true;
            }
        }


        if($first_line == false) $row .= "</tr></thead>";
        else $row .= "</tr>";

        $first_line=true;

        if ($is_empty) {
            continue;
        } else {
            echo $row;
        }
    }
    fclose($f);
    echo "\n</table>";
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent! :-) Is it possible to replace <td> with <th> when it's inside thead?
Fantastic, Haresh!! :-D
0

You can use a counter to detect that you're displaying the first line :

echo "<table class='table table-bordered'>\n\n";
$f = fopen("users.csv", "r");
$counter = 0;
while (($line = fgetcsv($f)) !== false) {
    if ($counter == 0) {
        $row .= "<thead><tr>";
        $is_empty = false;
        foreach ($line as $cell) {
            if ($cell !== '') {
                $row .= "<th>" . htmlspecialchars($cell) . "</th>";
            } else {
                $is_empty = true;
            }
        }
        $row .= "</tr></thead>\n";
    } else {
        $row .= "<tr>";
        $is_empty = false;
        foreach ($line as $cell) {
            if ($cell !== '') {
                $row .= "<td>" . htmlspecialchars($cell) . "</td>";
            } else {
                $is_empty = true;
            }
        }
        $row .= "</tr>\n";
    }

    $counter++;
    if ($is_empty) {
        continue;
    } else {
        echo $row;
    }
}
fclose($f);
echo "\n</table>";

Comments

0

Yes. If the 1st line is always the header you can use a counter for lines.

$rowCounter = 0;
while (($line = fgetcsv($f)) !== false) {
    $tr = '<tr>';
    $td = '<td>';
    $tr_end = '</tr>';
    $td_end = '</td>';
    if ( ++$rowCounter == 1 ){
        $tr = '<thead><tr>';
        $td = '<th>';
        $tr_end = '</tr></thead>';
        $td_end = '</th>';
    }
... your code ...
}

Now you can use these variables inside your code. There could be several other solutions.

Hope this helps!

Comments

0

It would be tidier if you generate the markup text separately using a function.

function tr($data, $head = false, $xssProtect = true){
    // Formatting
    $cellFmt = $head? "<th>%s</th>": "<td>%s</td>";
    $rowFmt = $head? "<thead><tr>%s</tr></thead>": "<tr>%s</tr>";
    $returnFmt = sprintf($rowFmt, str_repeat($cellFmt, count($data)));

    // XSS
    if ($xssProtect) $data = array_map('htmlspecialchars', $data);

    // Format and return output
    return vsprintf($returnFmt, $data);
}


echo "<table class='table table-bordered'>\n\n";
$f = fopen("users.csv", "r");
$line_no = 0;
while (($line = fgetcsv($f)) !== false) {
    echo tr($line, !$line_no);
    $line_no++;
}
fclose($f);
echo "\n</table>";

Comments

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.