1

I've searched other questions to get me this far, but I'm missing a key element to make my code work.

The csv file is formatted as:

job;customer;location;tech;side
Ex(12345;company;floor 1;John;Auto)

Below is my current function:

function readCSV($csvFile,$side){
$file_handle = fopen($csvFile,'r');
while (!feof($file_handle)) {
    $line_of_text[] = fgetcsv($file_handle, 1024,';');
}
fclose($file_handle);
return $line_of_text;
}

I want the function to only load the records that match the $side parm. (Example: Drive side, or Auto side)

Thanks in advance, I'm new to whole gathering data from csv.

1 Answer 1

1

side is the fifth column (index 4) of the CSV, so just check if it equals $side and if so add it to your array:

function readCSV($csvFile,$side){
    $file_handle = fopen($csvFile,'r');
    while (!feof($file_handle)) {
        //save temp line
        $line = fgetcsv($file_handle, 1024,';');
        //compare temp line forth column and if matches add line to array
        if ($line[4] == $side) {
            $line_of_text[] = $line;
        }
    }
    fclose($file_handle);
    return $line_of_text;    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.