douchen9569
douchen9569
2016-04-05 17:40
浏览 92
已采纳

如何使用PHP将.csv文件内容操作到HTML表中?

OK, here's what I've got so far:

<table class="tftable" border="1">
    <tr><th>Header 1</th><th>Header 2</th><th>Header 3</th><th>Header 4</th><th>Header 5</th><th>Header 6</th><th>Header 7</th></tr>
    <?php
        define('CSV_PATH','/Applications/XAMPP/xamppfiles/htdocs/SITEPREVIEW/csv_file_drop_folder/');
        $csv_file = CSV_PATH . "test.csv";
        if(($handle=fopen($csv_file,"r"))!== FALSE){
            fgetcsv($handle);
            while(($data=fgetcsv($handle,1000,","))!==FALSE){
                $num = count($data);
                for($c=0;$c<$num;$c++){
                    $col[$c]=$data[$c];
                }
                $col1=$col[0];
                $col2=$col[1];
                $col3=$col[2];
                $col4=$col[3];
                $col5=$col[4];
                $col6=$col[5];
                $col7=$col[6];

                echo "<tr><th>".$col1."</th><th>".$col2."</th><th>".$col3."</th><th>".$col4."</th><th>".$col5."</th><th>".$col6."</th><th>".$col7."</th></tr>";
            }
            fclose($handle);
        }
    ?>
</table>

So I run this and ($data=fgetcsv($handle,1000,",")) is coming out FALSE I think, but not sure why, and so only the headers get printed to the page.


Got rid of fgetcsv($handle);, but only prints one line, and I checked the file contents of my .csv file and this is what I have:

CSV FILE CONTENTS

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsjpqpdm620596
    dsjpqpdm620596 2016-04-05 18:17
    已采纳

    Get rid of this line:

    fgetcsv($handle);
    

    That is:

    if(($handle=fopen($csv_file,"r"))!== FALSE){
         while ( ( $data = fgetcsv($handle, 1000, ",") ) !== FALSE ) {
    

    If this only prints one line, check the line endings on the csv. Make sure they are /n or /r/n

    Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. http://php.net/manual/en/function.fgetcsv.php

    From which I'd assume the csv may have been generated in a mac. I am not positive, but I believe what was happening was that your first fgetcsv read the first (and only) line of your csv, so the second one, in the while loop, correctly noted there was no further line to read, so returned FALSE, thus terminating the loop before you started.

    If the csv is automatically generated, you'd want to correct this issue on that end of things, if it's created on say, a Mac, ideally the tool that created it is corrected to use unix style line endings. If that can't be readily done, just open the csv in a code editor, configured to save/convert line ending to unix style, and save it prior to loading.

    LF: Multics, Unix and Unix-like systems (Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS, and others[1]

    CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9, and OS-9

    https://en.wikipedia.org/wiki/Newline

    I was curious, it looks like Macs up to OS 9 used , but OS X uses as you'd expect. Note that windows uses which works fine since is ignored I believe and is present.

    点赞 评论

相关推荐