Jump to content

Recommended Posts

I have a form that asks for the users name and student number. Then in a separate script I going to test it against a txt file that looks like this: 

 

Jim Smith, 400424565
Sarah Hillier, 534712479
Jonathan Quinlan, 764134296
Keith Roberts, 123456789
Sarah Hillier, 200343656
Chloe Butler, 678123987
 
I am new to php so I am not sure where I went wrong. I keep getting warnings:
 

Notice: Undefined index: snumber in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 9

Warning: in_array() expects parameter 2 to be array, string given in /Applications/XAMPP/xamppfiles/htdocs/assignment1/enrolled.php on line 21

 

enrolled.php

  <?php

  $students = "student.txt"; // text file for students and student number

  //converting a string into a variable

  $name = $_POST["name"];
  $number = $_POST["snumber"];


//open student file and explode into an array

  $sfile = fopen($students, 'r+') or die ("Student file does not exist");

  while ($sline = fgets ($sfile)) {
    $list = explode(",", $sline);

    //test array against text input

    if (in_array($name, $list[0])) {

      if (in_array($number, $list[1])) {

        echo $name;
        echo $number;

      } // end of inner if

    } //end of outer if

  } // end of while



  fclose($sfile)



  ?>

P.S.

 

You should also note that $sline will contain the trailing linefeed character. You need to trim() the line before exploding otherwise $line[1] will also have the linefeed and will not, therefore, match the number.

I figured out what was wrong with the text name snumber. I forgot the equal sign on the html side of the form. 

 

Made the correction from list[1] to list. No errors. However without list[1] meaning the name how will it be compared to the txt file for name and number?

I was having someone help me but they are confusing me more than anything. So I do apologize for the questions. I have the code reading the text file perfectly. However as soon as I include the else statement it outputs it 6 times which I know is because its within the while statement. However when I leave it out of the while statement and create a new if statement it will always show the form

if ($name != $list[0] && $number != $list[1]) {

    include 'index.php';
  }
  while ($sline = fgets ($sfile)) {
    $list = explode(",",trim ($sline));

    //test array against text input

    if ($name == $list[0] && $number == $list[1]) {

      echo "Welcome $name $number";

    } else {

      include 'index.php';

    }

  } // end of while

  fclose($sfile)

  ?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.