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.

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

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿