0

I'm trying to write a simple if statement that should be returning true but is instead returning false. The issue I'm having is that for several items on my list $dataPeriod should be returning true matching '202265' I've tried converting it to a string thinking it didn't like that I was comparing a string to an int and that didn't seem to help.

I'm importing the data from this CSV file - https://www.harpercollege.edu/_resources/php/openclasslistweb.csv

<?php
$next = 0;
$file_to_read = fopen('openclasslistweb.csv', 'r');
 
if($file_to_read !== FALSE){
    echo "<table class = 'open-list-table'>\n";
    while(($data = fgetcsv($file_to_read, ',')) !== FALSE){
        for($i = 0; $i < count($data); $i++) {
            if ($i == 0){
                $dataCourse = $data[$i];
            }
            if ($i == 1){
                $dataCRN = $data[$i];
            }
            if ($i == 2){
                $dataTitle = $data[$i];
            }
            if ($i == 3){
                $dataMethod = $data[$i];
            }
            if ($i == 4){
                $dataInstructor = $data[$i];
            }           
            if ($i == 5){
                $dataStartDate = $data[$i];
            }
            if ($i == 6){
                $dataEndDate = $data[$i];
            }
            if ($i == 7){
                $dataMonday = $data[$i];
            }
            if ($i == 8){
                $dataTuesday = $data[$i];
            }               
            if ($i == 9){
                $dataWednesday = $data[$i];
            }
            if ($i == 10){
                $dataThursday = $data[$i];
            }
            if ($i == 11){
                $dataFriday = $data[$i];
            }
            if ($i == 12){
                $dataSaturday = $data[$i];
            }
            if ($i == 13){
                $dataSunday = $data[$i];
            }   
            if ($i == 14){
                $dataStartTime = $data[$i];
            }   
            if ($i == 15){
                $dataEndTime = $data[$i];
            }   
            if ($i == 16){
                $dataRoom = $data[$i];
            }                   
            if ($i == 17){
                $dataBuilding = $data[$i];
            }           
            if ($i == 18){
                $dataSchedule = $data[$i];
            }   
            if ($i == 19){
                $dataDesc = $data[$i];
            }   
            if ($i == 20){
                $dataPeriod = $data[$i];
            }               
        }
        if (strpos($dataPeriod, "202265") !== false) {
            if ($next == 0){
                echo "<tr class='open-list-header'>";
            }
            else {
                echo "<tr>";
            }
            echo "<td>" . $dataCourse . "</td>" . "<td>" . $dataCRN . "</td>" . "<td>" . $dataTitle . "</td>" . "<td>" . $dataMethod . "</td>" . "<td>" . $dataInstructor . "</td>" . "<td>" . $dataStartDate . "</td>" . "<td>" . $dataEndDate . "</td>" . "<td>" . $dataMonday . "</td>" . "<td>" . $dataTuesday . "</td>" . "<td>" . $dataWednesday . "</td>" . "<td>" . $dataThursday . "</td>" . "<td>" . $dataFriday . "</td>" . "<td>" . $dataSaturday . "</td>" . "<td>" . $dataSunday . "</td>" . "<td>" . $dataStartTime . "</td>" . "<td>" . $dataEndTime . "</td>" . "<td>" . $dataRoom . "</td>" . "<td>" . $dataBuilding . "</td>" . "<td>" . $dataSchedule . "</td>" . "<td>" . $dataDesc . "</td>" . "<td>" . $dataPeriod . "<td>";
            echo "</tr>\n";
            $next = $next + 1;
        }
    }
    echo "</table>\n"; 
    fclose($file_to_read);
}
 
?>
aynber
  • 22,380
  • 8
  • 50
  • 63
Wesley
  • 38
  • 4
  • does this answer your issue https://stackoverflow.com/questions/9139202/how-to-parse-a-csv-file-using-php ? – medilies Mar 02 '22 at 17:28
  • 1
    Oooof, that's painful. If the columns are always going to be the same, why are you iterating through the columns to assign the variables instead of just assigning them outright? `$dataCourse = $data[0]; $dataCRN = $data[1];` and so on – aynber Mar 02 '22 at 17:29
  • 1
    why don't you isolate your code by just calling `strpos($data[20], "202265")` ? I'm sure it will be easy to find the issue – Jack Mar 02 '22 at 17:29
  • Because I work with other people on the team and it makes it easier for them to follow. I totally understand what you mean. – Wesley Mar 02 '22 at 17:45
  • 1
    This seems like you need to practice some basic debugging skills - using a tool like [Xdebug](https://xdebug.org), or some simple `var_dump` lines, look at what is _actually_ in the variables rather than what you _expect_ to be in them. If you find a value where you still think it should be true, you can make a [mcve] which we can run without having to download an external CSV file. – IMSoP Mar 02 '22 at 17:56
  • You have 6 possibilities for $data[20] : string(31) "Academic Period", string(13) "202265", string(59) "Online ANYTIME (Asynchronous)", string(13) "202295", string(13) "202335", and string(7) "LL1". One of your possible matches might fall under one of the non-numerical strings instead. There is definitely something wonky with that CSV, since those strings are nowhere near close to the character count shown in the var_dump. – aynber Mar 02 '22 at 18:08
  • Here are the unique values of `$data[20]`: `{"0":"\u0000A\u0000c\u0000a\u0000d\u0000e\u0000m\u0000i\u0000c\u0000 \u0000P\u0000e\u0000r\u0000i\u0000o\u0000d\u0000","1":"\u00002\u00000\u00002\u00002\u00006\u00005\u0000","16":"\u0000O\u0000n\u0000l\u0000i\u0000n\u0000e\u0000 \u0000A\u0000N\u0000Y\u0000T\u0000I\u0000M\u0000E\u0000 \u0000(\u0000A\u0000s\u0000y\u0000n\u0000c\u0000h\u0000r\u0000o\u0000n\u0000o\u0000u\u0000s\u0000)\u0000","180":"\u00002\u00000\u00002\u00002\u00009\u00005\u0000","982":"\u00002\u00000\u00002\u00003\u00003\u00005\u0000","2104":"\u0000L\u0000L\u00001\u0000"}` – aynber Mar 02 '22 at 18:14
  • Yep it's 100% the CSV file. I converted it to columns and saved it as a normal csv and everything is working correctly. I think I need to work with our report team. Thanks, everyone. Aynber put your response in as the answer and I'll aprove. – Wesley Mar 02 '22 at 18:57

1 Answers1

0

Your CSV has a lot of invisible characters or encoded characters, so some of your values aren't what you expect. I was able to get all of the unique values for $data[20], and with json_encode it shows this:

{
    "0":"\u0000A\u0000c\u0000a\u0000d\u0000e\u0000m\u0000i\u0000c\u0000 \u0000P\u0000e\u0000r\u0000i\u0000o\u0000d\u0000",
    "1":"\u00002\u00000\u00002\u00002\u00006\u00005\u0000",
    "16":"\u0000O\u0000n\u0000l\u0000i\u0000n\u0000e\u0000 \u0000A\u0000N\u0000Y\u0000T\u0000I\u0000M\u0000E\u0000 \u0000(\u0000A\u0000s\u0000y\u0000n\u0000c\u0000h\u0000r\u0000o\u0000n\u0000o\u0000u\u0000s\u0000)\u0000",
    "180":"\u00002\u00000\u00002\u00002\u00009\u00005\u0000",
    "982":"\u00002\u00000\u00002\u00003\u00003\u00005\u0000",
    "2104":"\u0000L\u0000L\u00001\u0000"
}
aynber
  • 22,380
  • 8
  • 50
  • 63
  • 1
    The CSV file was using UTF-16. UTF-8 is what you're looking for if someone finds this post. – Wesley Mar 02 '22 at 19:44