使用 while 循环列出文件

发布于 2024-09-30 09:27:14 字数 1235 浏览 0 评论 0原文

我无法让它正常工作,我需要程序列出文件中的记录,如果 $records[$row][2] 与之前的相同,它应该使用新的 $records 重复类“field” [$row][2],否则应该开始一个新的#row。请帮忙!

if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {


    $prevRow2 = '';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);

        $records[] = $data;

        echo 'Previous'. $prevRow2;
        echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";

        if ($records[$row][2] == $prevRow2) {

            for ($c=0; $c < $num; $c++) {
            if ($c != 1) {  
                echo "<div class=\"field\">" . $data[$c] . "</div>";
                }   
            }
            $prevRow2 = $records[$row][2];
            $row++;
        }
        else {
            echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
            for ($c=0; $c < $num; $c++) {
            if ($c != 1) {  
                echo "<div class=\"field\">" . $data[$c] . "</div>";
                }   
            }
            $prevRow2 = $records[$row][2];
            $row++;
            echo "</div>";
        }




    echo "</div>";
}
fclose($handle);

}

I cannot get this to work properly, I need the program to list through the records within the file, if $records[$row][2]is the same as the previous, it should repeat class 'field' with a new $records[$row][2], otherwise it should start a new #row. Please help!

if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {


    $prevRow2 = '';

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);

        $records[] = $data;

        echo 'Previous'. $prevRow2;
        echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";

        if ($records[$row][2] == $prevRow2) {

            for ($c=0; $c < $num; $c++) {
            if ($c != 1) {  
                echo "<div class=\"field\">" . $data[$c] . "</div>";
                }   
            }
            $prevRow2 = $records[$row][2];
            $row++;
        }
        else {
            echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
            for ($c=0; $c < $num; $c++) {
            if ($c != 1) {  
                echo "<div class=\"field\">" . $data[$c] . "</div>";
                }   
            }
            $prevRow2 = $records[$row][2];
            $row++;
            echo "</div>";
        }




    echo "</div>";
}
fclose($handle);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2024-10-07 09:27:14

首先,您似乎假设您的 CSV 文件是按您想要分组的列排序的。如果不是这种情况,那么您应该首先读取文件,对其进行排序,然后打印它。如果您的 CSV 文件对于服务器的内存来说不是太大,那么这应该可行。

另外,您的代码有重复并将整个文件存储到一个数组中(实际上,就像我上面建议的那样,但我不知道这是不是故意的)。尝试这样的事情:

这是我的 demo.csv:

"Banana", "yellow"
"Lemon", "yellow"
"Orange", "orange"
"Strawberry", "red"
"Tomato", "red"

这是循环遍历它并按颜色列出它的 PHP:

<?php

if (($handle = fopen('demo.csv', "r"))) {
    $prev = false;
    while (($data = fgetcsv($handle, 1000, ","))) {
        if ($prev !== $data[1]) {
            $prev = $data[1];
            echo '<p><b>' . $prev . '</b></p>';
        }
        echo $data[0] . '<br>';
    }
}
fclose($handle);

?>

这是 HTML 中的输出(我在此处添加了一些换行符以便正确显示)

<p><b>yellow</b></p>
Banana<br>
Lemon<br>
<p><b>orange</b></p>
Orange<br>
<p><b>red</b></p>
Strawberry<br>
Tomato<br>

(如果 CSV 未按颜色,您需要不同的方法。)

First of all, you seem to assume that your CSV file is ordered by the column you want to group on. If this is not the case, then you should first read the file, sort it, and then print it. If your CSV file is not too big for the memory of your server, this should work.

Also, your code has repetitions and store the entire file into an array (actually, just like I suggested above, but I don't know if it is intentional). try something like this:

This is my demo.csv:

"Banana", "yellow"
"Lemon", "yellow"
"Orange", "orange"
"Strawberry", "red"
"Tomato", "red"

This is the PHP to loop through it and list it by color:

<?php

if (($handle = fopen('demo.csv', "r"))) {
    $prev = false;
    while (($data = fgetcsv($handle, 1000, ","))) {
        if ($prev !== $data[1]) {
            $prev = $data[1];
            echo '<p><b>' . $prev . '</b></p>';
        }
        echo $data[0] . '<br>';
    }
}
fclose($handle);

?>

This is the output in HTML (I added some linebreaks for proper display here)

<p><b>yellow</b></p>
Banana<br>
Lemon<br>
<p><b>orange</b></p>
Orange<br>
<p><b>red</b></p>
Strawberry<br>
Tomato<br>

(If the CSV was not grouped by color, you'd need a different approach.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文