MatthewPatten Posted October 29, 2014 Share Posted October 29, 2014 So I'm basically done an assignment, however I'm having the biggest brainfart at the moment. For the life of me I cannot remember how to "jump line" in a file and have to put "end" at the end of my txt files. Also I have to put a break in my code which messes up my formatting: $coursedata = explode(' ', rtrim($coursedata)); $studentdata = explode(' ', rtrim($studentdata)); fwrite($fp, $number.'||'.$_POST['course'].' '.$content); Here is the main code below for the script: <html> <head> <?php $success = 0; $number_of_registered = 0; $course_array = getdatafile('courses.txt'); $registered_array = getdatafile('register.txt'); #______________global variables____________________ function getdatafile($file) { # code... $coursedata = file_get_contents($file); $coursedata = explode(' ', rtrim($coursedata)); unset($coursedata[count($coursedata) - 1]); $course_array = array(); foreach($coursedata as $row) { $course_array[] = explode('||', $row); } return $course_array; } #_____________functions_______________________ if (isset ($_POST['name']) and isset ($_POST['number']) and isset ($_POST['course'])) { #________________test if the post exists $name = $_POST['name']; $number = $_POST['number']; $found = 0; $findregistered = 0; for ($i = 0; $i < sizeof($registered_array); $i++) { if ($registered_array[$i][0] == $_POST['number'] && $registered_array[$i][1] == $_POST['course']) { # ___________test if the student is enrolled in this course $findregistered = 1; } if($registered_array[$i][1] == $_POST['course']) { #________counter of the number registered in this course $number_of_registered++; } } for ($i = 0; $i < sizeof($course_array); $i++) { if ($course_array[$i][1] == $_POST['course']) { # ______________this test if the number of registered is the max $course_name = $course_array[$i][0]; $findcourse = 1; if($number_of_registered < $course_array[$i][2]) { $number_max = 0; } else { $number_max = 1; } } } $studentdata = file_get_contents('students.txt'); $studentdata = explode(' ', rtrim($studentdata)); for ($i = 0; $i < sizeof($studentdata); $i++) { list($studentname, $studentnumber) = explode("||", rtrim($studentdata[$i])); if (strtoupper($name) == strtoupper($studentname) && strtoupper($number) == strtoupper($studentnumber)) { $found = 1; if ($findcourse == 1 && $findregistered == 0 && $number_max == 0) { # _____________________do all the tests to record the student in the course("register.txt") and do it if all the test are checked $success = 1; $content = file_get_contents('register.txt'); $fp = fopen('register.txt', 'w'); fwrite($fp, $number.'||'.$_POST['course'].' '.$content); fclose($fp); echo "<title>".strtoupper($name).",".strtoupper($studentname)."</title>"; } } } } ?> <style> body, p{padding: 0;margin: 0;} </style> </head> <body> <form action="index.php" method="POST"> <?php if (isset ($_POST['course'])) { $course = htmlentities ($_POST['course']); # ___________________print the information of rgester in the screen for if ($success == 1) { echo "You have successfully enrolled in $course_name, $course!"; } if($found == 0) { echo 'The credentials you have entered are incorrect, please try again.'; } else { if($findregistered == 1) { echo 'You have already enrolled in this course.'; } else { if($number_max == 1) { echo "Sorry $course_name, $course is full, please select another course."; } } } } ?> <p>Courses</p> <p><input name = "name" placeholder = "Student Name" type = "text"></p> <p><input name = "number" placeholder = "Student Number" type = "text"></p> <p><select name = "course" size = "1"> <option id = "select">-- Select Course --</option> <?php for ($i = 0; $i <sizeof($course_array); $i++) { $number_of_registered1 = 0; for ($j = 0; $j < sizeof($registered_array); $j++) { if($registered_array[$j][1] == $course_array[$i][1]) { $number_of_registered1++; } } $a = '<option value = "'.$course_array[$i][1].'">'.$course_array[$i][0].' registered:'.$number_of_registered1.'</option>'; echo $a; } ?> </select></p> <p><input type = "submit" value = "Send"><input type = "reset" value = "Clear"></p> </form> </body> </html> courses.txt register.txt students.txt index.php Quote Link to comment Share on other sites More sharing options...
Barand Posted October 29, 2014 Share Posted October 29, 2014 Are you talking about "\n"? If so, instead of file_get_contents(), use file() which returns an array of the lines in the file. Quote Link to comment Share on other sites More sharing options...
MatthewPatten Posted October 29, 2014 Author Share Posted October 29, 2014 If I change that, I get this error Warning: rtrim() expects parameter 1 to be string, array given in /Library/WebServer/Documents/index.php on line 12 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted October 29, 2014 Solution Share Posted October 29, 2014 Yes, you now have an array of lines instead of one long string try function getdatafile($file) { # code... $coursedata = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $course_array = array(); foreach($coursedata as $row) { $course_array[] = explode('||', $row); } return $course_array; } Quote Link to comment Share on other sites More sharing options...
MatthewPatten Posted October 29, 2014 Author Share Posted October 29, 2014 Thank you good Sir, this has seemed to have resolved my issue. Quote Link to comment 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.