Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey I don't really have an error for this one but if I want to check if user input is in the csv my code stops working. no errors, nothing and I don't understand why??
so this is the backend
PHP
<pre><?php
session_start();

// onderscheid maken tussen adressen met of zonder toevoeging
if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
    $Zipcode = $_POST['Zipcode'];
    $Housenumber = $_POST['Housenumber'];
    $Toevoeging = $_POST['Toevoeging'];
    $Beschrijving = $_POST['Beschrijving'];

    //toevoeging en beschrijving zijn geen verplichte velden
    if(!empty($Zipcode) && !empty($Housenumber)){ 
      // //de csv check stopte de hele code??
      // Set the path to your CSV file
      $csv_file = "NewPostcodesCheckt.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
          echo "User input found!";
          break 2; // Exit the while and foreach loops
          }elseif($item == $zipcode && $item == $Housenumber && $Toevoeging){
            echo "User input found!";
          }else{
            echo "No user input found";
          }
        }
      }

      // Close the file handle
      fclose($file_handle);
      //infraorders moet nog worden toegevoegd

        $input_xml = '<?xml version="1.0" encoding="UTF-8"?>
        <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <env:Header/>
          <env:Body>
            <grexxRequest>
              <header>
                <reference>Example zipcodecheck</reference>
              </header>
              <requestData>
                <ZipCodeCheckRequest>
                  <Portfolio>Teleworker</Portfolio>
                  <ZipCode>' . $Zipcode . '</ZipCode>
                  <HouseNr>' . $Housenumber . '</HouseNr>
                  <HouseNrExtension/>
                </ZipCodeCheckRequest>
              </requestData>
            </grexxRequest>
          </env:Body>
        </env:Envelope>';
        
       
        $url = "https://service.grexx.today/interfaces/routit/75yo9a83fc60f1y06bt9/zipcodecheck";

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/xml"));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_USERPWD, "yu8zg3adky1ywdf6srbzi2zfpatxm0ef:Va9`+nh&niwZzsE?xR88LpUhSGB)Vv83");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
        curl_setopt($ch, CURLOPT_POST, 1);
        $result = curl_exec($ch);

        // Curl
        $xml = simplexml_load_string($result);
        $array = array();
        $xml->registerXPathNamespace('a', 'http://schemas.grexx.net/atalanta/connect');

        foreach($xml->xpath('/soap:Envelope/soap:Body/a:grexxResponse/a:responseData/a:ZipCodeCheckResponse/a:AvailableSuppliers') as $event) {
            array_push($array, $event);
        }
        $jsonstring = json_encode($array);

        echo $jsonstring;
    }else{
        //echo "<script>alert('Vul eerst de verplichte velden in voordat je hier komt ');</script>";
        // dit nog een error laten zien in de json code van index.php
        echo "error";
    }
}else{
    header("Location: index.php");
}


if the csv code is deactivated the whole code works just fine.

What I have tried:

I'm not good with csvs so i tried looking up csv check in php and afther a long search i got this. tried to ask google about this problem but I just get more information about javascript
Posted
Updated 28-Apr-23 0:46am

Getting your code to run at all does not mean it is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
PHP
function double($a)
{
    return $a * $a;
}


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Rebecca2002 28-Apr-23 6:28am    
didn't know they had that in vscode aswell so tried it out but the terminal says there are no problems detected in the work space??
OriginalGriff 28-Apr-23 7:19am    
Debuggers aren't passive tools: they allow you to look at what is happening while your code runs. That lets you see what path it is taking and why, what variables contain and compare that to what you expected to happen. That lets you know where the problem is so you can fix it.
Your csv handling is incorrect, as shown in the following code:
PHP
foreach ($row_data as $item) {
   // Check if the item matches either of the user inputs
   if ($item == $zipcode && $item == $Housenumber) {

The $item field cannot be equal to the two variables at the same time, it can only be one or the other. You should process the data as an array, as shown at PHP: fgetcsv - Manual[^]. Since you know the structure of your csv data you can access the various fields by array offsets.
 
Share this answer
 
Comments
Rebecca2002 28-Apr-23 7:07am    
but I need both of the $zipcode and $housenumber to be in the same row? because there are about 10 of the same zipcodes in the file that match up with the corresponding house number
Richard MacCutchan 28-Apr-23 7:12am    
Yes I understand that, but a single variable ($item) cannot hold two different values at the same time. You need to read each line of csv data and address the different fields by their offset values. Go to the link I provided above, which gives a complete example.

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