Values search with PHP - Is it possible?

3

Is it possible to access a .CSV file, capture a value and search it in another .CSV file ?!

Example:
Note: The images below are from .xlsb files, but for demonstration purposes only, I am using .csv files

The code should in the case store a value and the search in another .CSV file, in my case, would be equal to these images:

link link

It will look for the value "80283022" in the worksheet, save it and look for it in the "items" worksheet and when you find it (on line 155) it will get the value of column G that is "6" .

Is it possible?!

I have a small code that accesses .csv files and collects some data.

<?php
$file1 = __DIR__ . '/download/Trabalhos.csv';
$csv1 = file($file1);
foreach ($csv1 as $row1 => $line1) {
    $row1++;
    $column1 = str_getcsv($line1, ';');
    if ($row1 == 2) {
        $column1[6]."<br>";
        $valor1 = $column1[6];
    }
}
?>
    
asked by anonymous 26.09.2017 / 18:11

1 answer

1

I made this function you enter with the file that you want to search, the column where you will search the value and the column where the value and the search value are returned.

<?php
function pesquisaCsv($arquivo, $coluna_pesquisa, $coluna_resultado, $valor){
    if (($handle = fopen($arquivo, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            if($data[$coluna_pesquisa] == $valor){
                return $data[$coluna_pesquisa];
            }
        }
        fclose($handle);
    }
}

$file1 = __DIR__ . '/download/Trabalhos.csv';
$csv1 = file($file1);
foreach ($csv1 as $row1 => $line1) {
    $row1++;
    $column1 = str_getcsv($line1, ';');
    if ($row1 == 2) {
        $column1[6]."<br>";
        $valor1 = $column1[6];
        pesquisaCsv('/download/outro_arquivo.csv', 0, 6, $valor1)
    }
}
?>
    
26.09.2017 / 18:31