Sunday, 18 August 2013

Using fgetcsv and fopen issue with looping never exiting

Using fgetcsv and fopen issue with looping never exiting

There's an issue with this piece of code it never gets to the "Done" echo.
for($current_day=1;$current_day<=10;$current_day++){
echo("lists/Day ".$current_day.".csv </br>");
$person_data = "";
if ( ( $handle = fopen( "lists/Day ".$current_day.".csv", "r" ) ) !==
FALSE ) {
while ( ( $data = fgetcsv( $handle, 1000, ",", "//" ) ) !== FALSE ) {
if ( $data[2]!="Grade" ) {
$collection = $db->students;
$person_data['last_name'] = trim( $data[0] );
$person_data['first_name'] = trim( $data[1] );
$person_data['fullname'] = $person_data['first_name'].'
'.$person_data['last_name'];
//var_dump($person_data);
$cursor =
$collection->findOne(array('fullname'=>$person_data['first_name'].'
'.$person_data['last_name']));
if($cursor!=null){
echo("UPDATE");
if($cursor['frees'][$current_day-1] != 1){
$cursor['frees'][$current_day-1] = 1;
echo 'Updated: '.$data[0] . " - ". $data[1] . " -
". $data[2] . "</br>" ;
$collection->save($cursor);
}
}
else{
echo("ADD");
//addperson($data,$current_day);
}
}
// do something with the data here
//echo $data[0] . " - ". $data[1] . " - ". $data[2] . "</br>" ;
}
fclose( $handle );
echo("Done");
}
else{
echo "FALSE";
}
}
outputs
1
lists/Day 1.csv
ADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADDADD
Which is correct but never loops to the second csv

No comments:

Post a Comment