Writing in a text file

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Thu Nov 09, 2017 11:57 am

Hi, I am very new to php and this is the first time I have written php. So I do apologize if the questions I am asking seem simple.

I am creating a course registration system. So far I have the form which is index.php which asks for the student name, number and to select a course from a drop down menu. Once submitted the users are taken to enrolled.php where I have compared the name and number entered to the student name and number on a txt file. From there I am trying to take the student name, number and course they selected and write it to a txt file to be called upon and displayed on enrolled.php where the user after hitting submit will see their name, student number and the courses they are enrolled in and then repeat the drop down menu until they have selected all courses they want.

Each time a student is changed however I want the txt file to be written over with the new data.

So my question is based on my code, where am I going wrong in getting the information written to the load.txt file?

index.php

Code: Select all

<!DOCTYPE html>
<html>
  <body>
    <h1>Course Selection</h1><br>


    <form action="enrolled.php" method="post">


              Name: <input type="text" name="name" placeholder="Name" required="required" maxlength="50">
              <br><br>

              Student Number: <input type="text" name= "snumber" required="required" maxlength="9">
              <br><br>

        <?php
        //form

      $coursedata = "course.txt"; // text file coursedata
      echo "Select a course: <select name = \"pcourse\">\n";

      $cfile = fopen ($coursedata, 'r+') or die ("File does not exist or you do not have permission");

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

      echo " <option value =\"$drop[0]\">$drop[0] $drop[1] $drop[2]</option>\n";

      }

      fclose ($cfile);

      echo " </select>\n";

      ?>
      <br><br>
      <input type = "submit" value="submit" name= "submit">

    </form>

    </body>
    </html>
enrolled.php

Code: Select all

<?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");

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

    //test array against text input

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

      $found = 1;

      //load name, number and course selected into load.txt

      $handle = fopen ('load.txt', 'w+');
      fwrite ($handle, $name, $number, $cfile."\n");
      fclose ($handle);

      $readin = file ('load.txt'); 
    }

  } // end of while

  if (!$found) {

    include 'index.php';
  }

  fclose($sfile)

  ?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Thu Nov 09, 2017 1:16 pm

You haven't really explained how the script is not working, however I can assume a few things and hopefully point a few things out:

in enrolled.php, you write and then load the 'load.txt' file, but do not use $readin variable.

Once you have found a match in the while loop, you continue to loop; you can use the break function to exit this loop.

If a match is found, nothing happens, where as if there is not a match, the index.php page will load.

'load.txt' can only be used for one student.

You are reading a file line by line to create an array, this seems to be a bit of hard work to me.

--------------------------

File handling in php can be difficult using the older file access functions (file, fgets etc.), I would suggest that you learn and use a few of php functions which can help you:

file_get_contents & file_put_contents allows you to just read or write a variable to a file.

serialize & unserialize these allow you to store variables in a form that can be written or read from files.

If you generate multi dimensional arrays in your script and use them to modify the data, you should find it a lot easier.
Last edited by hyper on Thu Nov 09, 2017 1:43 pm, edited 1 time in total.

bldb
New php-forum User
New php-forum User
Posts: 8
Joined: Thu Nov 09, 2017 11:46 am

Thu Nov 09, 2017 1:39 pm

So my problem is that when I test the file I get a bunch of errors:

Warning: fwrite() expects at most 3 parameters, 4 given
Warning: fclose() expects parameter 1 to be resource, boolean given
Notice: Undefined variable: cfile
Warning: fopen(load.txt): failed to open stream: Permission denied

I have attempted to understand what you have posted. I have inserted a break at the end of the while loop and I have used file_get_contents. I am reading up on serialize and am assuming you are saying I need to use serialize to get the array to become a storable variable to put into the load.txt file.

Code: Select all

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

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

    //test array against text input

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

      $found = 1;

      //load name, number and course selected into load.txt

      $handle = fopen ('load.txt', 'a');
      fwrite ($handle, $name, $number, $cfile."\n");
      fclose ($handle);

      $readin = file_get_contents ('load.txt', true);
      break;

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 526
Joined: Mon Feb 22, 2016 5:52 pm

Thu Nov 09, 2017 1:46 pm

Using file_get_contents with unserialize and file_put_contents with serialize means that you don't need the loop, read the links.

Post Reply