简体   繁体   English

选择一些 csv 值以使用 php 在 html 表中导出

[英]select some csv values to export in html table with php

I've copied this php:我已经复制了这个 php:

<?Php 
echo "<html><body><table border=1>";
$f = fopen("datos.csv", "r");
$fr = fread($f, filesize("datos.csv"));
fclose($f);
$lines = array();
$lines = explode("\r\n",$fr); // IMPORTANT the delimiter here just the "new line" \r\n, use what     u need instead of... 

for($i=0;$i<count($lines);$i++) {
    echo "<tr>";
    $cells = array(); 
    $cells = explode(",",$lines[$i]); // use the cell/row delimiter what u need!
    for($k=0;$k<count($cells);$k++) {
        echo "<td>".$cells[$k]."</td>";
    }
    // for k end
    echo "</tr>";
}
// for i end
echo "</table></body></html>";
?> 

This code generates the html table.此代码生成 html 表。 I have a csv with one column with diferent values separated with comma, and I want only some values.我有一个 csv,其中一列用逗号分隔不同的值,我只想要一些值。 The question is the loop with the variable $k to catch only value2, value4, value7 ,... I appreciate some help, I'm learning php but I start in advanced mode :-( and I search this site but don't find it.问题是带有变量 $k 的循环只捕获 value2, value4, value7 ,... 我感谢一些帮助,我正在学习 php 但我从高级模式开始 :-( 我搜索了这个网站,但没有找到它。

Its always dangerous to read a whole file into memory, eventually you will find a file that is large enough to blow the script up because it is Too Large to read into memory.将整个文件读入内存总是危险的,最终你会发现一个大到足以炸毁脚本的文件,因为它太大而无法读入内存。

Use fgetcsv() instead in this situation.在这种情况下使用fgetcsv()代替。 This reads one line at a time from your csv file, into an array, all you then have to do is pick the occurances (columns) you actually want to process.这一次从您的 csv 文件中读取一行,放入一个数组中,然后您要做的就是选择您实际想要处理的事件(列)。

if (($handle = fopen("datos.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 2000, ",")) !== FALSE) {

        // pick the columns you are interested in
        // remember arrays start at index 0
        echo sprintf('The bits I am interested in %s, %s, %s<br>',
                        $data[2], $data[4], $data[7]);
    }
    fclose($handle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM