简体   繁体   English

循环无法正常运行

[英]loop not functioning properly

I am unable to get this loop to function properly. 我无法使此循环正常运行。 Whenever ($records[$row][2] == $prevRow2) I need it to recreate the class=field and only close the div=row that's part of the group where ($records[$row][2] == $prevRow2) . 每当($records[$row][2] == $prevRow2)我都需要它来重新创建class = field并仅关闭div = row,它是组中的一部分,其中($records[$row][2] == $prevRow2) Please help!! 请帮忙!!

    if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
        $prevRow2 = false;
                $row=0;
        while (($data = fgetcsv($handle, 1000, ","))) {
            $num = count($data);
            $records[] = $data;
            echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
            echo 'Is '. $prevRow2 . 'the same as ' .$records[$row][2];

            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++;
                echo "<div id=\"filler\"></div>";

            }//if close

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

fclose($handle);

} }

$ row在您创建$ row ++的行之前似乎为空;

Some output and/or further description of your data would really help, but I notice a few things. 某些输出和/或对数据的进一步描述确实会有所帮助,但我注意到一些事情。

1 - Your closing if and else do exactly the same thing except for this filler div. 1-您的结束if和else执行完全相同的操作,除了该填充div。 I get the impression this is not what you desire, but I don't really understand enough to suggest what to do. 我觉得这不是您想要的,但是我对建议的理解还不够。

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

This code is common to both the if and else blocks, so it executes no matter what. 该代码对于if和else块都是通用的,因此无论执行什么都将执行。 Is that what you want? 那是你要的吗? If so, you might as well move it out of the if and move only the filler div into the if, nothing in the else. 如果是这样,则最好将其从if中移出,仅将填充div移至if中,否则不移。

2 - What is the purpose of $records and $records[$row]. 2-$ records和$ records [$ row]的用途是什么。 As far as I can see, $records[$row] === $data whenever its used. 据我所知,只要使用$ records [$ row] === $ data。

3 - The div with id=row is closed at the end of every while loop. 3-id = row的div在每个while循环结束时关闭。 You seem to suggest you don't want this, put if you only do it when $records[$row][2] AKA $data[2] matches the previous iteration's, I don't think your HTML will be well formed. 您似乎建议您不要这样做,如果仅在$ records [$ row] [2]又称为$ data [2]与先前的迭代匹配时这样做,我认为您的HTML格式不正确。

4 - try using foreach instead of your foor loops 4-尝试使用foreach而不是foor循环

It looks like Company is actually index 3, not 2. 看起来Company实际上是索引3,而不是2。

  $prevData3 = false; $row=0; while (($data = fgetcsv($handle, 1000, ","))) { if ($data[2] != $prevData2) { if ($prevData2) echo '</div>'; echo "<div id=\\"row\\"><div id=\\"num\\">" .$row. "</div>"; row++; } else echo "<div id=\\"filler\\"></div>"; foreach ($data as $d) { if ($c != 1) { echo "<div class=\\"field\\">" . $d . "</div>"; } } echo "<br />"; $prevData2 = $data[2]; } 

echo CLOSE_DIV; 回声CLOSE_DIV;

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

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