Nested For-Loops in PHP with fgetcsv

204 Views Asked by At

I'm pretty sure you are going to take one look at this and laugh, but I dont fully understand how fgetcsv works, I just know it retrieves the contents of the csv file I am pointing at. I dont understand the auto-increment of it. So in the first few lines of the function, I am checking to see if today's checklist exists. If it doesnot, I call the newChecklist function I created that creates the file, and populates the first line.

After that I am trying to create a table cell by cell using the fgetcsv, and a nested for loop.

I think it is my nested for loops causing the problem, but the way they are interacting is weird. the output essentially looks like this:

   -------- Line 1 --------
   -------- Line 1 --------
   -------- Line 2 --------
   -------- Line 1 --------
   -------- Line 2 --------
   -------- Line 3 --------
   -------- Line 1 --------
   -------- Line 2 --------
   -------- Line 3 --------
   -------- Line 4 --------
   -------- Line 1 --------
   -------- Line 2 --------
   -------- Line 3 --------
   -------- Line 4 --------
   -------- Line 5 --------

Here is the code in question:

function viewCheckList($today){
    if(!file_exists("QA_Checklists/FileName_QA_Checklist_".$today.".csv")){
        newCheckList($today); //other function
    }
    $fp=fopen("QA_Checklists/FileName_Checklist_".$today.".csv",'r');       
    $line="";
    $output="";
    $output = "<table>";
    for($x=0;$x<5;$x++){
        $rQuickRef=fgetcsv($fp,1024);
        $line.="";
        $line.="<tr><td>";
        for($i=0; $i<19; $i++){
        $line.= $rQuickRef[$i];
        $line.="</td><td>";
        }
        $line.="</td></tr>";
        $output.=$line;
    }
    $output.="</table>";
    echo$output;

}

?>
0

There are 0 best solutions below