Home > Mobile >  if statement not returning true even though it should
if statement not returning true even though it should

Time:03-03

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);
}
 
?>

CodePudding user response:

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"
}
  • Related