Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So I'm busy to compare user input with my csv
but I get this error and tbh I don't understand why
Warning: foreach() argument must be of type array|object, bool given 


now hear me out I know that it says that the argument needs to be an array|bool but I checkt my $row_data if it's an array with the is_array() function and it said yes it's an array.
so now I'm lost why would it not work if my argument is an array??

// //de csv check stopte de hele code??
// // Set the path to your CSV file
$csv_file = "checkcsvfileforuserinput.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
  $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
    $_SESSION['input_found'] = "user input found";

    }elseif($item == $Zipcode || $item == $Housenumber || $item == $Toevoeging){
      $_SESSION['input_found'] = "user input found";

    }else{
      $_SESSION['input_found'] = "user input not found";

    }
  }
}


What I have tried:

to begin with I did the is_array to check if $row_data really is an array and it said yes.
tried looking on internet but they have examples of just reading the csv files with the
1000, ","
next in the
$row_data = fgetcsv($file_handle);
So I tried that but with ; idk what the 1000 is for but the ; is the separator that I have. however this is not showing anything different
Posted
Updated 1-May-23 0:43am

If you look at the documentation: PHP fgetcsv() Function[^] it shows the return value:
Quote:
Return Value: An array with the CSV fields on success, NULL if an invalid file is supplied, FALSE on other errors and on EOF
So if there are errors in your CSV file, or it can't be found then $row_data can be a bool

I'd start with the debugger and find out exactly what you are passing to the function, and exactly what it returns.
 
Share this answer
 
Changing the test from AND (&&) to OR (<coe rde="">||) your loop is still incorrect. If one item in the row contains the correct Zipcode then you will report the input as correct. But you have not checked that both correct fields exist in the same row. As I explained in your previous question at Csv check breaks the whole whole code?[^], you need to access both fields at the same time and test them together, thus:
PHP
// Read the contents of the file
while (($row_data = fgetcsv($file_handle)) !== FALSE) {
  if ($row_data[0] == $Zipcode && $row_data[1] == $Housenumber && $row_data[2] == $Toevoeging)) { // NB check which actual fields contain the relevant items.
    // User input found in the CSV
    $_SESSION['input_found'] = "user input found";
  }
}

Go back to my previous answer and follow the link to the PHP manual where array access is explained.
 
Share this answer
 
v2

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