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.

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

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误