Chapter 6 Persistence-Saving and Retrieving Data 954240 – Web Programming
Introduction In this chapter you will learn to distinguish between temporary (transient) data and data that is stored (persistent data), and compare the use of text files and databases for persistent data storage. You will learn the basic operations needed to process text files, and implement these operations using PHP functions to read, write and append text files located on a Web server. You will also learn how to extract (parse) multiple data values from a single line that has been read from a text file.
The Difference Between Persistent and Transient Data Persistent Data is known as data whose life extends beyond the lifetime of any of the program. Persistent Data is stored so that it can be accessed and modified by programs as needed. Transient Data is known as the world of computing , data that is used and then forgotten. More precisely transient data refers to data that is produced while an application is running and lost when the application ends
The Difference Between Persistent and Transient Data Many programs work with a combination of transient and persistent data. For example, you might write a program that asks the user for information of some kind and then stores the information in a file for later use. In this case the user inputs and screen displays may be considered transient data, while the data that the program writes to the file is persistent since it will remain in the file after the program ends.
Working with Text File In order to work with a text file, a program must first open the file. There are basically three ways to open a text file: Opening a file for Read Operations Opening a File for Write Operations Opening a File for Append Operations
Working with Text File Opening a file for Read Operations Assuming the file exists, this operation opens the file and positions a read pointer at the beginning of the file. Opening a File for Write Operations You will open a file for write operations when you want to replace data in an existing file with new data. Opening a File for Append Operations You will open a file for append operations when you want to add data to an existing file.
Closing a Text File When a program that has opened a file no longer needs to access it, the file should be closed. The close operation places an End-Of-File (EOF) marker at the end of the file and releases the file for access by other programs.
Reading Data from a Text File Let’s start with an example where we open a text file, read data from the file, close the file, and then process the data. Consider a file named scores.txt that contains five scores on separate lines as follows: 89 77 92 69 87
Reading Data from a Text File Here is a program requirement to process the scores.txt file: Here is a solution algorithm for averageScore.php: averageScore requirement: Read the five scores from the scores.txt file, then calculate and display the average score. averageScore.php algorithm: Open scores.txt as scoresFile for reading Read scorel, score2, score3, score4, score5 from scoresFile Close scoresFile avgScore = (scorel+ score2+ score3+ score4+ score5) / 5 Display averageScore END
Reading Data from a Text File Open : use Open to indicate that the program must open a file and specify whether the file is to be opened for reading, writing or appending Read : use Read to indicate that the program must read a value from a file into a variable. Close : use Close to indicate that the program must close a file.
PHP Functions to Read Data from a Text File Hear is the PHP code for the averageScore algorithm: <?php $scoresFile = fopen (“scores.txt”,”r”); $score1 = fgets ($scoresFile); $score2 = fgets ($scoresFile); $score3 = fgets ($scoresFile); $score4 = fgets ($scoresFile); $score5 = fgets ($scoresFile); fclose ($scoresFile); $avgScore = ($score1 + $score2 + $score3 + $score4 + $score5) / 5; echo (“<h1>AVERAGE SCORE</h1>); echo (“<p>The average score is $avgScore.</p>”); echo (“<p><a href=\”averageScore.html\”> Return to averageScore form</a></p>); ?>
PHP Functions to Read Data from a Text File $scoresFile = fopen(“scores.txt”, “r”); This instruction uses the PHP fopen( ) function to open a file. The first parameter “scores.txt” indicates the name of the file to open. The second parameter “r” in this case the file is to be opened for reading (“r”), with the read pointer set to the beginning of the file. $score1 = fgets ($scoresFile); The fgets( ) function reads and returns the next line from the file.
PHP Functions to Read Data from a Text File Mode Meaning r Open file for reading only r+ Open file for reading and writing w Open file for writing to replace existing data if no existing file, it will create new file. w+ Open file for reading to replace existing data if no existing file, it will create new file. a Open file for writing at the end of existing file if no existing file, it will create new file. a+ Open file for reading at the end of existing file if no existing file, it will create new file.
PHP Functions to Read Data from a Text File fclose ($scoresFile); The fclose( ) function is used to close the file. It is good programming practice to close a file as soon as your program has finished using it. This ensures that the file is available for processing by other programs as soon as possible.
PHP Functions to Read Data from a Text File
PHP Functions to Write Data to a Text File fputs ($scoresFile, “$score1\n”); The fputs( ) function is used to write data to a file The \n represents the new line character and is an example of an escape character. fputs ($scoresFile, “$score1\n$score2\n$score3\n$score4\n$score5\n”); The value from each variable is written to the file followed by a new line marker, so the values are written on five separate lines.
Using Escape Characters Here are the most commonly used escape characters: \t generates a tab \n starts a new line \” generates a double quote “ \’ generates a single quote ‘ \\ generates a back slash \