简体   繁体   中英

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) . 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. 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. 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.

2 - What is the purpose of $records and $records[$row]. As far as I can see, $records[$row] === $data whenever its used.

3 - The div with id=row is closed at the end of every while loop. 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.

4 - try using foreach instead of your foor loops

It looks like Company is actually index 3, not 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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