Search code examples
phpcsvfgetcsv

How to search a CSV file and return multiple entries based on keyword input


Hello I have a CSV file that has Name, Extension, and Cell. Some users have both extensions and a cell number. I am trying to create a search on our intranet page where a user will enter a keyword (name, ext or cell) and then return all the results that have the keyword in it. I read in a CSV file using fgetcsv but I was only able to return extensions. If I searched for a name or a cell number it would return a blank white page. here is the code I have:

$value = $_POST["search"];
$file = 'phonelist/PhonelistCSV2.csv';
while (($data = fgetcsv($file)) !== FALSE){
    //print_r($data); - this was just a test to see the data printing out. It prints the entire CSV
    print_r(array_keys($data,$value));
}
fclose($file);

I gave up on fgetcsv after an entire day of googling and trying to get it working. Now I am trying taking the CSV and encoding it to JSON with

$csv=file_get_contents($file); $array=array_map("str_getcsv", explode ("\n",$csv)); 
$json=json_encode($array);

I printed out the json array to verify that the contents were the same as the csv. The I created a javascript function to search for a keyword. I was testing with putting the value in a div just for testing purposes but I can't get the function to work

$value = $_POST["search"];
$keyword=json_encode($value);
<script type="text/javascript">
function search(keyword){
return json.filter(({name, ext, cell}) => {
    return name.toLowerCase().indexOf(keyword.toLowerCase()) > -1 ||
           ext.toLowerCase().indexOf(keyword.toLowerCase()) > -1 ||
           cell.toLowerCase().indexOf(keyword.toLowerCase()) > -1 
})
}
document.getElementById('test').innerHTML = json; // I tried it this way and
alert(search($keyword);
</script>
<div id="test"></div>

so long story short, I need some help in creating a search for my intranet to search a CSV office directory with names, extension and cell numbers. so far, in the solutions I've tried I've only been able to return extension numbers. I would like it to function like, search for "Tim", return all people with Tim in their name and their subsequent extension, cell # or both.


Solution

  • You could just do a case insensitive search on something like the name, and filter your records accordingly.

    The first part just gets the csv data into an array.

    <?php
    
    $data =<<<DATA
    Neil Buchanan,334,012345678
    Tony Hart,314,01234828828
    Henri Matisse,392,01234457575
    DATA;
    
    $data = array_map('str_getcsv', preg_split('/\R/',$data));
    
    $name_search = function($keyword) use ($data) {
        return array_filter($data, function($line) use ($keyword) {
            return stristr($line[0], $keyword);
        });
    };
    
    $results = $name_search('to');
    var_export($results);
    echo "Next Search\n";
    $results = $name_search('he');
    var_export($results);
    

    Output:

    array (
        1 => 
        array (
          0 => 'Tony Hart',
          1 => '314',
          2 => '01234828828',
        ),
      )Next Search
      array (
        2 => 
        array (
          0 => 'Henri Matisse',
          1 => '392',
          2 => '01234457575',
        ),
      )