Jump to content

Jump Line in File


Go to solution Solved by Barand,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/292148-jump-line-in-file/
Share on other sites

  • Solution

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;
}
Link to comment
https://forums.phpfreaks.com/topic/292148-jump-line-in-file/#findComment-1495194
Share on other sites

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.