Kyle Gagnon
Kyle Gagnon

Reputation: 62

PHP Reading Files and Additions

I have question with PHP. So I am making a form where you submit your name and it looks at the file and tells you are registered.

$file = "Website.txt";
$filehandle = fopen($file, "r");
while (!feof($filehandle)) {
    $data = fgets($filehandle);
    $str = explode(",", $data);
    for ($i = 0;$i = 2;$i++) {
        echo $str[$i];
        if ($str[$i] == "Kyle") {
            echo "You are registared!";
        } else {
            echo "You are not registared!";
        }
    }
}
fclose($filehandle);

When I run the code it is not working. Please help!

Upvotes: 0

Views: 48

Answers (2)

NateDSaint
NateDSaint

Reputation: 1524

From the code it's hard to tell if this is looking correctly at your file, but if I were you I'd simplify the code by doing this:

$file = "Website.txt";
$data = file_get_contents($file);
$array = explode(",",$data);
foreach ($array as $item) {
  if ($item == "Kyle") {
    echo "You're registered!";
  }
}

NOTE: code not tested yet.

I'd also recommend using a regular expression or at least stripping the whitespace from the item you're comparing, like this:

if (preg_match("/needle/",$haystack)) {
}

//or 

if (trim($haystack) == "needle") {
}

Upvotes: 0

aadam
aadam

Reputation: 713

You have a typo in for loop. Second part is an assignment not a comparison.

Upvotes: 1

Related Questions