user2788650
user2788650

Reputation:

looping thru csv file only iterates once with feof and fgetcsv

Im parsing a csv file to get certain fields and modify them but the problem is that for some reason the feof only iterates once. I did some testing and I realized that if I remove the fgetcsv line the file is read until the end of file. Herebelow is my code. Any help would be greatly appreciated.

<?php
include 'property-features.php';

//------------- get lat and long --------------//
function geocode($address){

    // url encode the address
$address = urlencode($address);

$json = file_get_contents('http://open.mapquestapi.com/geocoding/v1/address?key={mykey}&location='.$address);
$jsonArr = json_decode($json);

$lat1 = $jsonArr->results[0]->locations[0]->latLng->lat;
$lon1 = $jsonArr->results[0]->locations[0]->latLng->lng;

        // verify if data is complete
    if($lat1 && $lon1){

            // put the data in the array
        $data_arr = array();

        array_push(
            $data_arr,
            $lat1,
            $lon1,
            $address
        );

        return $data_arr;

        }else{
            return false;
        }
}
/* ------------- fix property address --------------- */
function ordinal($num) {
    $ones = $num % 10;
    $tens = floor($num / 10) % 10;
    if ($tens == 1) {
        $suff = "th";
    } else {
        switch ($ones) {
            case 1 : $suff = "st"; break;
            case 2 : $suff = "nd"; break;
            case 3 : $suff = "rd"; break;
            default : $suff = "th";
        }
    }
    return $num . $suff;
}


/* ------------------ Open original mls feed csv and create a csv file ------------------*/
$file_handle = fopen("sefl_data.csv", "r");
$file = fopen("/home/javy1103/public_html/wp-content/uploads/wpallimport/files/mlsFeed.csv", "w");
while (!feof($file_handle)) {

    echo "string";

    $line_of_text = fgetcsv($file_handle);
    $photos = intval($line_of_text[88]);

   if(1 == 1){

    /* ------------------ get parking spaces ------------------*/

        $line_of_text[84] = str_replace($patterns, $replacement, $line_of_text[84]);
        if(substr_count($line_of_text[84], "1 parking") || substr_count($line_of_text[84], "1 car garage")){
            $line_of_text[95] = 1;
        }else if (substr_count($line_of_text[84], "2 parking") || substr_count($line_of_text[84], "2 car garage")){
            $line_of_text[95] = 2;
        }else if (substr_count($line_of_text[84], "3 parking") || substr_count($line_of_text[84], "3 car garage")){
            $line_of_text[95] = 3;
        }else if(substr_count($line_of_text[84], "3 parking or more parking") || substr_count($line_of_text[84], "3 or more car")) {
            $line_of_text[95] = "3+";
        }

    /* ---------------- Get latitude and longitude -------------------*/

        if(!empty($line_of_text[23])){

        $stNum = preg_replace("/[^0-9]/","",$line_of_text[21]);
        echo $stNum.'<BR>';

        $address = $line_of_text[20].' '.$line_of_text[23].' '.$line_of_text[21].','.$line_of_text[27].',FL,'.$line_of_text[29];
        }else {$address = $line_of_text[20].' '.$line_of_text[21].','.$line_of_text[27].',FL,'.$line_of_text[29];}

        //$latLong = geocode($address);
        //$line_of_text[25] = $latLong[0].', '.$latLong[1];

        $line_of_text[96] = "";
        $counter = 2;
        //unset($line); 
        $url = $line_of_text[89];
        //$line[0] = $url;

        while ($counter <= $photos && $counter < 15) {
            $photoNumber = '_'.($counter).'.jpg';
            $line_of_text[96+$counter] = substr_replace($url, $photoNumber, sizeof($url) - 5, sizeof($photos)+4);
            $counter++;
        }
    }
}
fclose($file_handle);
fclose($file);
?>

Upvotes: 0

Views: 701

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

Hazarding a guess, and quoting from the PHP docs

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

Upvotes: 0

Related Questions