PHP Read And Write CSV File Format



Folder Structure:


index.php

<!DOCTYPE html>
<html>
<head>
    <title>Web Application.</title>
</head>
<body>
    <?php 

   /* $list = array (
        array('aaa', 'bbb', 'ccc', 'dddd'),
        array('aaa', 'bbb', 'ccc', 'dddd'),
        array('aaa', 'bbb', 'ccc', 'dddd')
        );

    $fp = fopen('file.csv', 'w');

    foreach ($list as $fields) {
        fputcsv($fp, $fields);
    }*/

    function ddataReadcsv($filename, $header=false) {
        $handle = fopen($filename, "r");
        echo '<table border=1>';
        if ($header) {
            $csvcontents = fgetcsv($handle);
            echo '<tr>';
            foreach ($csvcontents as $headercolumn) {
                echo "<th>$headercolumn</th>";
            }
            echo '</tr>';
        }
        while ($csvcontents = fgetcsv($handle)) {
            echo '<tr>';
            foreach ($csvcontents as $column) {
                echo "<td>$column</td>";
            }
            echo '</tr>';
        }
        echo '</table>';
        fclose($handle);
    }
    ddataReadcsv('file.csv',true);
    ?>
</body>
</html>

Output:

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment