Click here to Skip to main content
15,943,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to find out in my users input is in the csvs but its not really working. I need an easy and quick way for this because my files are pretty long. i found this code:
// Set the path to your CSV file
$csv_file = "CheckUserInformationData.csv";

// Open the file for reading
$file_handle = fopen($csv_file, "r");

// Read the contents of the file
while (!feof($file_handle)) {
// Get the current row of data from the CSV je moet niet de 0 en de ; vergeten

    if ( $row_data = fgetcsv($file_handle)){
            // Loop through each item in the row
            foreach ($row_data as $item) {
                // Check if the item matches either of the user inputs
                    if ($item == $Zipcode && $item == $Housenumber) {
                    // User input found in the CSV
                        echo "<br>";
                        echo "User input found!";
                        break 2; // Exit the while and foreach loops
                        echo "<br>";
                        echo "user input not found";
                        break 2;
        echo "It does not work";


and now i'm confused because doesnt fgetcsv() make the csv in an array??
what I did found out is that he puts a whole csv row into on array objects and then tries to compare that I tried inarray but that just says not found

What I have tried:

looking on stack over flow and on google. with csvs you mostly get anwsers for python
Updated 18-Apr-23 23:29pm
Member 15627495 19-Apr-23 4:05am    
$item is Array Type.
echo $item[0] ; // will provide datas 
Rebecca2002 19-Apr-23 5:16am    
yes but when I type that it gives me 8?? idk why it would give me 8? and if I do echo $item; it shows the first whole row of the csv

1 solution

Check the documentation:
Returns an indexed array containing the fields read on success, or false on failure.
The error message tells us that the function returned false, meaning that it failed to parse the current line as CSV.

You need to check the content of your file to see why the function is failing. If you want to ignore invalid lines, check that the function did not return false before trying to foreach over the return value.

if ($item == $Zipcode && $item == $Housenumber)
As discussed in the comments, the only way the $item variable can be equal to both the $Zipcode and $Housenumber variables at the same time is if the $Zipcode and $Housenumber variables contain the same value.

If you want to test whether the item is equal to either of the user inputs, as your comment suggests, you need to use OR, not AND:
if ($item == $Zipcode || $item == $Housenumber)

$item is the first row of the csv for some reason??
According to the documentation, the $item variable should be an array of the fields extracted from the current row of the CSV file. If that's not the case, then there's something wrong with your CSV file.

Since we don't have access to your file, you're going to need to check the contents of the file yourself.
Share this answer
Rebecca2002 19-Apr-23 5:28am    
I dont know why but it's not showing the error now. only problem is that its still not properly checking if user input is in the csv file
Richard Deeming 19-Apr-23 5:43am    
Probably because of this:
if ($item == $Zipcode && $item == $Housenumber)

That will only match if the item is the same as the zip-code AND the house number.

Which can only be the case if the zip-code and house number are the same value.

I suspect you want an "or" instead of an "and" there:
if ($item == $Zipcode || $item == $Housenumber)
Rebecca2002 19-Apr-23 6:10am    
yes but isnt thats what i want? because I have multiple of the same zipcodes in my file. so both of the housenumber and the zipcode need to be in the same row in the csv for it to be true.
example csv
6483 XP Zipcode
34 housenumer
6483 EJ zipcode
45 Housenumber

user types in
6483 XP zipcode
45 housenumber

then this has to give false because that adres doesn't exist
Richard Deeming 19-Apr-23 6:17am    
Unless the zipcode and house number are the same value, then $item cannot be equal to both values at the same time.

Your comment says you want to match "if the item matches either of the user inputs". That means you need to use OR, not AND.
Rebecca2002 19-Apr-23 6:22am    
ah thanks its still not working tough because $item is the first row of the csv for some reason?? <-- not sure why as it should contain the whole csv. and it compares it to just the zipcode or house number
so it always gives wrong trying to find something out about that. Like with in_array I guess.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900