Putting data from a csv into php variables

Asked

Viewed 32 times

-2

Hello, I have a php code that takes the data of the file csv inserted by the user, it shows everything in the same variable, I need to separate each column in different variables.

The code is putting all cvs content in only one variable "$category", need to fun what has in the category column of csv (first category) in the variable category what has in the second column (value) in the second variable $value

if (isset($_POST['importSubmit'])) {

        $csvMimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain');

        if (!empty($_FILES['file']['name']) && in_array($_FILES['file']['type'], $csvMimes)) {

            if (is_uploaded_file($_FILES['file']['tmp_name'])) {

                $csvFile = fopen($_FILES['file']['tmp_name'], 'r');


                fgetcsv($csvFile);

                while (($line = fgetcsv($csvFile)) !== FALSE) {

                    $categoria   = $line[0];
                    echo "Cat: " . $categoria ?? null;
                    $valor_cat  = $line[1];
                    echo "Valor: " . $valor_cat ?? null;
                }
            }


            fclose($csvFile);
        }
    }

Imagem de como esta exibindo

  • The question is not clear. The code already does what it is asking. It does not, specify what the problem is which is the expected result.

1 answer

-2

Browser other questions tagged

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