lundi 13 août 2018

PHP code supposed to check a file for an ID not working correctly

So I'm working on a PHP app receiving a student ID from an input and searching through a file to find if the ID is present or not. If it's present, the name of the student and whether hes present or not should be printed out, and if the ID doesn't exist, it should say that there are no students with this ID. Here is an example of what the txt file looks like:

1234|Sally Simon|1 
4321|Larry Lonbottom|0 
7364|Hannah Harrow|1

I made a solution and tested it, but it prints out a student name even when the ID I enter is false. Could someone give me an idea of what I'm doing wrong? Here is my code:

<?php 
$student=$_GET["student"];
$lines = file('students.txt');

foreach ($lines as $line){
    $var = explode("|", $line);

}

    if($student == $var[0] || $var[2] == "1"){
        echo "$var[1]($var[0]): has signed up";
    }else if($student == $var[0] || $var[2] == "0"){
        echo "$var[1]($var[0]): hasn't signed up";
    }else{
        echo "No students with that id!";
    }
?>

Aucun commentaire:

Enregistrer un commentaire