PHP File Handling

I am not that experienced in PHP and I have a lot to learn yet. For an assignment I have to make a student grade book that includes three pages. The viewing page I got to work. The user inputs the first and last name of a student that is located in a .txt file. The information in the txt file is split by a : by using an explode function. When the correct first and last names are used in the text input fields the information linked to the students name is printed into an HTML data table.

On an additional page I am required to update or add a new row within that table of students after an update button is clicked. Basically the information that is typed within the text fields will write to the txt file to store the information while splitting the info up with a :. I am not sure how to go about this.

Okay. How do you think that process would work? What do you think would be involved?

The process I know would be a lot easier to use MySQL to the store the information and to add/delete rows to a table from what I understand. That is required for the next project however. I am somewhat familiar with the fopen function that I used on the student grade book viewing page and accessing the text information from the txt file. I am just unsure how to add/delete a row of information to a txt document with the use of an html form field. I know that fwrite function can be used to write data to the txt document but I am not sure how to structure it in this case.

The structure of the txt document is similar to the following

Firstname:Lastname:Hw1:Hw2:Test1:Test2

each of these are individual array elements on my viewing page. The viewing page echos out these elements in an HTML table but I don’t believe that that is necessary for the table update page.


I recommend that instead of typing in names, you open/read through the .txt file and make a list of the existing names, then produce a select/option menu so that someone can just select an existing name, rather than to type it in with the potential for typos not even finding a name. You would use this select/option list for the delete and update operations. Having a list (array) of the existing names in your code will also let you validate any new name, to make sure it doesn’t already exist.

Next, you have shown an ‘add’ column form field and column name and value form fields. If you are going to have multiple named columns, you should store each piece of information as - column_name:value. You would separate each piece of information using a different delimiter, such as a | (pipe.) The data structure would become - Firstname:Lastname|column_name1:value1|column_name2:value2|…

If you are going to have multiple named columns, you should input and store the column names into a separate .txt file. This will allow you to produce a select/option list when you need to pick which column of data to operate on. Creating new columns should be a separate process from the data entry form.

The easiest way to actually update or delete the row of data, would be to read the entire contents of the .txt file into a php array, with the main array index being the composite Firstname:Lastname value, and the array’s data being an array of the exploded name:value pairs. To delete the row, just delete the matching entry in the php array, then write the remaining data back to the .txt file. To update the row, replace the correct value, by using the column name to index into the array, then write the data back to the .txt file.

I agree mostly with phdr. I would use a single form for students however, and just just buttons to differentiate the purpose.

Thanks everyone for your responses. You brought up some very good and helpful points. I agree that phdr’s method would be a more efficient way to go. Unfortunately the instructor of my class requires that the project to be done in the way that I mentioned. So I cannot use a select option menu sadly and I have to work with the one txt document called gradebook.txt that stores existing names already that can be modified or added to using the multiple form fields.

Sponsor our Newsletter | Privacy Policy | Terms of Service