How to find out which line is a CSV data

0

Good afternoon, I have a problem that I do not know how to solve, it will probably be in PHP but I have no idea how to do this. More precisely what I need to do is: I have a website, and in it I will create a separate page, on this separate page the user will put his / her CPF, then after placing the CPF the system must identify his name and generate a credential so he can print ... Anyone have any idea how I know the name of the person using only his or her CPF search in the CSV?

  • I do not have the CSV here, but it is separated like this: NAME, AGE, CPF, PROFESSION *
asked by anonymous 12.01.2018 / 17:29

1 answer

2

You can do the following:

$CPF_user = $_POST['CPF']; // receber cpf do user
$lines = file('file.csv'); // array com as linhas do file.csv
foreach($lines as $l) { // percorrer as linhas
    $params = explode(',', $l); // dividir linha pelo separador de colunas
    if($params[2] == $CPF_user) {
        $name_user = $params[0]; // caso seja encontrado o $name_user fica definido
        break; // escusado continuar a percorrer as linhas
    }
}
if(isset($name_user)) {
    // encontrado
}
    
12.01.2018 / 17:48