Jump to content

Keeps printing the same line into the text file


belbin09
 Share

Recommended Posts

Using text files as per teachers instructions. I am trying to read the load.txt file and then if the student isn't already registered for the course then it will print to load.txt. The problem I am having is that the same course is getting printed to load.txt file even if on index.php I select a different course. Then when I changed students if gives me the echo statement of already being registered in the course. 

 

Its printing the student number, and comp-3006.

 

Here is the txt file:

 

English||COMP-1004||4
Web Development||COMP-6002||5
Object-Oriented Java||COMP-1006||3
Networking Essentials||COMP-1035||4
Dynamic Websites AMP||COMP-3006||2
<?php

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

//converting a string into a variable

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

$coursedata = "course.txt"; // text file coursedata
$cfile = fopen ($coursedata, 'r');

while ($line = fgets ($cfile)) {
  $drop = explode ('||', trim ($line));

}

  fclose ($cfile);

//open student file and explode into an array

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

$found = 0;

// turning students into an array to read

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

  //test array against text input

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

    $found = 1;

    //load number and course selected into load.txt;

    $handle = fopen ('load.txt', 'r');
    while ($loadf = fgets ($handle)) {
      $data = explode (',', trim($loadf));

      if ($data[0] == $number && $data[1] != $drop[1]) {

    } else {

      echo "$number already registers for $drop[1]";

    } // end of else 
  } // end of while 41

  fclose ($handle);

    $handle = fopen('load.txt', 'a');

    fwrite($handle, "$number, $drop[1] \n");

  fclose ($handle);

    include 'load.txt';

    break;

  } // end of if 34

} // end of while

if (!$found) {

  include 'index.php';
}

fclose($sfile);

?>

Link to comment
Share on other sites

see my reply in your other current thread.

 

your current logic doesn't make any sense and won't work. you need to first define what inputs you have and what you are trying to do based on those inputs, before you write any code. your current code isn't even using the selected course number from the form in the test to find if the student is already registered for a course.

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.