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

如何使用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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?