How to find out which line is any CSV data

Asked

Viewed 59 times

0

Good afternoon, I have a problem that I do not know how to solve, probably will 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 CPF, there after putting the CPF the system has to identify what his name and generate a credential to be able to print ... Anyone have any idea how I’ll know the name of the person using only her social security number search ?

  • I’m not with the CSV here, but it’s separated like this: NAME, AGE, CPF, PROFESSION *
  • It would help if you put an example with a few lines of this csv format

  • @Miguel I completed the question.

1 answer

2


Here’s what you can do:

$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
}
  • 1

    Thank you very much !!

  • @Pabloabreu nothing, I’m glad you solved

Browser other questions tagged

You are not signed in. Login or sign up in order to post.