belbin09 Posted November 12, 2017 Share Posted November 12, 2017 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/305639-keeps-printing-the-same-line-into-the-text-file/ Share on other sites More sharing options...
mac_gyver Posted November 12, 2017 Share Posted November 12, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/305639-keeps-printing-the-same-line-into-the-text-file/#findComment-1553713 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.