Download
a web based introduction to programming n.
Skip this Video
Loading SlideShow in 5 Seconds..
A Web-Based Introduction to Programming PowerPoint Presentation
Download Presentation
A Web-Based Introduction to Programming

A Web-Based Introduction to Programming

0 Vues Download Presentation
Télécharger la présentation

A Web-Based Introduction to Programming

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. A Web-Based Introduction to Programming Chapter 06 Persistent Data – Working with Files and Databases Chapter 06

  2. Intended Learning Outcomes • Distinguish between transient and persistent data. • Describe the advantages of remote data storage. • Contrast the use of text files with a DBMS for data storage. • List the basic operations that can be performed on a text file. • Use fopen(), fgets(), and fclose() to read data. • Use fopen(), fputs(), and fclose() to write data. • Recognize and utilize escape characters in text output. • Use fopen(), fputs(), and fclose() to append data. • Use explode() and list() to parse character strings. • Apply PHP file-handling functions to process multiple files. Chapter 06

  3. Introduction • So far input and output has been associated with the user interface: • Input from HTML forms. • Output to new HTML pages using print() statements. • A program can receive input from other sources: • Data files, databases, microphones, scanners, robots, satellites, machines, other programs, .. • Output can be directed to other destinations: • Data files, databases, printers, e-mail messages, robots, machines, other programs, .. Chapter 06

  4. Introduction • In order to interact with an input or output device a program must typically: • Open a connection • Send or receive data through the open connection • Close the connection • In this chapter we learn how to work with data stored in files. • First we need to understand the difference between persistent and transient data Chapter 06

  5. Persistent and Transient Data • Transient data is used and then lost (temporary) : • Most conversations. • The display on a computer monitor. • Values stored in variables while a program is executing. • Persistent data that is stored for later use: • Information that you write down. • Data stored in a file or database. Chapter 06

  6. Working with Persistent Data • Different programs may work on the same file. • Consider a data file containing employee data: • Program A allows a clerical worker to add, delete, modify, or lookup the data in the file. • Program B reads the file, calculates the weekly wage for each employee, and creates paychecks. • Program C reads the file and calculates the total wages, average wage, highest wage and lowest wage to create a report for managers. • Program D reads the file and calculates a 5% increase for all employees. This is a planning tool for managers. Chapter 06

  7. Network Server Program A Search/update the file EMPLOYEE DATA FILE: Chris:Smith:20:15.75 Mary:Peters:40:18.00 Mike:Jones:35:12.50 Ann:Roberts:25:10.75 John:King:25:15.75 Ken:Stewart:32:12.50 Joan:Silvers:20:12.50 Karen:King:30:12.50 Client for Program A Server Program B Print paychecks Client for Program B Server Program C Report Client for Program C Server Program D Planning tool Client for Program D A client/server approach Chapter 06

  8. Advantages of Client/Server.. • Data files are stored on a secure server: • Easy to backup and maintain. • The programs that process the data files are also stored on the server: • Easily to modify the programs. • Multiple programs can easily access the data files. • New programs can be added for new purposes. • Different users access programs for their needs: • A manager might have access to Programs C or D. • A payroll office might have access to Program B. • An admin. assistant might have access to Program A. Chapter 06

  9. Files and Databases • Text files are useful for simple data storage: • Text files can be easily be read in a text editor. • Text files can be easily processed by any program. • Databases provide a more structured and efficient solution. • The most widely used type of database is a relational database which stores data in tables consisting of rows (records) and columns (fields). • Relational databases are much more efficient and secure for storing, managing, accessing, and modifying data. Chapter 06

  10. Storing Data in a Text File • This text file example contains employee data. • Each line contains a first name, last name, hours worked, and hourly wage: Chris:Smith:20:15.75 Mary:Peters:40:18.00 Mike:Jones:35:12.50 Ann:Roberts:25:10.75 John:King:25:15.75 Ken:Stewart:32:12.50 Joan:Silvers:20:12.50 Karen:King:30:12.50 Chapter 06

  11. The Same Data in a Database Table Chapter 06

  12. The Same Data in a Database Table Each table column contains a field. Each table row contains a record. Chapter 06

  13. Features of Relational Databases • Database tables are designed to avoid duplication of information and simplify management. • A Database Management System (DBMS) is software that provides a full range of data management functions, for example: • Add, modify, delete tables, records and fields. • Perform queries. • Generate reports. • Programs connect to the DBMS and use the functions provided – so the programmer does not need to develop this code. Chapter 06

  14. Our Approach.. • Files and databases are both important storage solutions for persistent data. • The PHP language is designed to work effectively with both. • Database management is an extensive topic. • In this chapter we will learn to work with text files. • Databases will be introduced in Chapter 14. Chapter 06

  15. Opening a Text File • There are three ways to open a text file: • Open a file for readoperations. • Open a file for writeoperations. • Open a file for appendoperations. Chapter 06

  16. Opening a Text File for ReadOperations • Allows the program to read data from a file (input) • When a program opens a file for reading: • A read pointer is positioned at the beginning of the file. • Each read operation reads one line from the file • After each read operation the read pointer advances to the start of the next line in the file. Chapter 06

  17. Opening a Text File for Write Operations • Allows the program to write data to the file (output) • Always creates a new file! • A file already in the folder with the same name is lost! • When a file is opened for writing: • A write pointer is set to the beginning of the file. • Each write operation adds data to the file at the location of the write pointer. • After each write operation, the write pointer advances to the end of the added data. Chapter 06

  18. Opening a Text File for AppendOperations • Allows the program to add data to the end of an existing file (output). • If the files does not already exist it is created. • When a file is opened for appending: • The write pointer is set to the end of the data already in the file, or at the beginning of the file if the file was just created. • Each write operation adds data to the file at the location of the write pointer. • After each write operation, the write pointer advances to the end of the added data. Chapter 06

  19. Closing a Text File • When a program that finished working with a file, the file must be closed. • The close operation places an end-of-file marker at the end of the file and releases the file for access by other programs. • If the file is not closed, it may be corrupted and the contents lost. Chapter 06

  20. Working Efficiently with Files • Data files may need be accessed frequently by multiple programs for different purposes. • On a busy Web site, hundreds or thousands of programs may need to access the same file within a short period of time. • It is good practice to open and close files efficiently so the file becomes available for other programs: • Open a file only when the program is ready to work with it. • Close the file as soon as the program no longer needs it. Chapter 06

  21. Reading Data from a Text File scores.txt: 89 77 92 69 87 averageScore requirement: read the five scores from the scores.txt file, then calculate and display the average score. Chapter 06

  22. Algorithm for averageScore.php averageScore.php algorithm: Open scores.txt as scoresFile for reading Read score1, score2, score3, score4, score5 from scoresFile Close scoresFile avgScore = (score1 + score2 + score3 + score4 + score5) / 5 Display averageScore END Chapter 06

  23. New PseudocodeTerms • Open scores.txt as scoresFile for reading: • Indicates that we are opening a file. • Provides the file name (scores.txt) and the variable name the program will use to represent the file (scoresFile) • Specifies whether to open the file for read, write or append operations. • Read score1, score2, score3, score4, score5 from scoresFile: • Indicates that we are reading values from a file into variables. • Close scoresFile: • Indicates that we are closing a file. Chapter 06

  24. Code for averageScore.php <?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; print (" <h1>AVERAGE SCORE</h1>"); print("<p>The average score is $avgScore. </p>"); print (" <p><a href = \"averageScore.html\">Return to averageScore form</a></p> "); ?> Chapter 06

  25. Code for averageScore.php $scoresFile = fopen("scores.txt","r"); • Uses the PHP fopen() function to open the file. • The first parameter "scores.txt" indicates the name of the file to open. • The second parameter "r" indicates how the file is to be opened ("r", "w" or "a"). • The fopen() call returns a file reference (file handle) which is assigned to the variable named $scoresFile. • The $scoresFile variable should be used in all subsequent statements to refer to the file. Chapter 06

  26. Code for averageScore.php $score1 = fgets( $scoresFile ); • The fgets() function reads and returns an entireline from the file. • Specify the variable that contains the file reference (in this case, $scoresFile), so that fgets() knows which file to read. • The fgets() function returns the data stored in the current line (in this case the value 89) which is assigned to the $score1 variable. • Once the line has been read, the read marker advances to the start of the next line in the file. Chapter 06

  27. Code for averageScore.php $score2 = fgets($scoresFile ); $score3 = fgets($scoresFile ); $score4 = fgets($scoresFile ); $score5 = fgets($scoresFile ); • Each statement uses fgets() to read the next line from the file and assign the value to a variable. • Once all the values have been read from the file into variables, the file can be closed. Chapter 06

  28. Code for averageScore.php fclose( $scoresFile ); • The fclose() function is used to close the file • Note the file is closed as soon as it is no longer needed by the program. • This is good programming practice: the file is now available for access by other programs. Chapter 06

  29. Code for averageScore.php $avgScore = ($score1 + $score2 + $score3 + $score4 + $score5) / 5; print("<p>The average score is $avgScore.</p>"); • These are statements you are already familiar with. • The average is calculated and stored in $avgScore. • The print() function outputs a paragraph to display the average score Chapter 06

  30. Screens for averageScore.php Chapter 06

  31. Writing Data to Text Files writeScores requirement: Write a program that asks the user for five student scores and the name of a file to store them. The program should receive the scores, store them in the file that the user specified (each score on a separate line) and report back to the user. (NOTE: We will need to use an HTML form to get the input..) Chapter 06

  32. Algorithm for writeScores.html writeScores.html algorithm: Prompt the user for score1, score2, score3, score4, score5 Get score1, score2, score3, score4, score5 Prompt the user for the name of the file to store the scores Get fileName Submit score1, score2, score3, score4, score5, fileName to writeScores.php END Chapter 06

  33. Screen for writeScores.html • The code is available in the samples folder, and provided in the textbook. • Note the paragraph that warns the user to be careful not to over-write a file! Chapter 06

  34. Algorithm for writeScores.php writeScores.php algorithm: Receive score1, score2, score3, score4, score5, fileName from writeScores.html Open fileName as scoresFile for writing Write score1, score2, score3, score4, score5 to scoresFile Close scoresFile Display "File Created" message to user END Chapter 06

  35. Code for writeScores.php • First retrieve the values from the HTML form: <?php $fileName = $_POST['fileName']; $score1 = $_POST['score1']; $score2 = $_POST['score2']; $score3 = $_POST['score3']; $score4 = $_POST['score4']; $score5 = $_POST['score5']; …. Chapter 06

  36. Code for writeScores.php • Next open the requested file for writing, write the five scores to the file, and close the file: $scoresFile =fopen("$fileName","w"); fputs($scoresFile, "$score1\n"); fputs($scoresFile, "$score2\n"); fputs($scoresFile, "$score3\n"); fputs($scoresFile, "$score4\n"); fputs($scoresFile, "$score5\n"); fclose($scoresFile ); …. • Let's examine fputs($scoresFile, "$score1\n"); … Chapter 06

  37. Code for writeScores.php fputs($scoresFile, "$score1\n"); • The fputs() function is used to write data to a file. • The 1st argument is the variable that refers to the file. • The 2nd argument is the character string to be written to the file: "$score1\n". • This character string contains the value stored in the $score1 variable, followed by the newline character \n • The fputs() function does not automatically add a new line to the file - if you want to add a new line, include \n • The new line character \n is an example of an escapecharacter – more on these shortly.. Chapter 06

  38. Code for writeScores.php • The remaining statements in writeScores.php provide feedback to the user: print (" <h1>The following scores have been stored in $fileName:</h1>"); print("<p>$score1<br />$score2<br />$score3<br /> $score4<br />$score5</p>");   print (" <p><a href = \"writeScores.html\">Return to writeScores form</a></p> "); ?> Chapter 06

  39. Screens for writeScores Chapter 06

  40. Avoid Security Holes! • In this example the user is invited to provide the name for a file that will be opened for write operations. This is a security hole! • The user can enter any file path and file name. • If the requested file already exists, it is replaced! • So the user can maliciously destroy any file on the disk. • The security hole could be avoided: • Specify a file name directly in your fopen() statement, with no user input. • Provide the user with a drop down list of file names. Chapter 06

  41. Using Escape Characters • We used \n to write a new line to the output file • For example: fputs($scoresFile, "$score1\n"); • Escape characters cannot be specified directly on the keyboard. Instead the character is indicated by a letter, preceded by a backslash \ tab: \t new line: \n double quote: \" single quote: \' back slash: \\ Chapter 06

  42. Using Escape Characters • \" is needed in print and fputs() statements when you need to include a double quote in a character string that begins and ends with double quotes. • \' is needed in print and fputs() statements when you need to include a single quote in a character string that begins and ends with single quotes. • \\ is needed when a backslash is to be output, since a single backslash indicates an escape character. Chapter 06

  43. Example of Escape Characters $fputs($someFile, "He said \"That\'s fine\"\n\t, and then left.\n\nThe End."); • This will write the following to the file (note the double quotes, single quote, new lines and tab): He said "That's fine" and then left. The End. Chapter 06

  44. Appending Data to Text Files • Appending data to files is similar to writing data to files • With an append operation, if the file already exists the data will be added to the end of the existing file content. • Existing files are not lost with append operations • We often need to add data to existing files, for example: • Log files that maintain a record of operations, such as a printer log file or mileage log. • Error files are used to report errors. • Files that contain regular equipment readouts or survey responses. Chapter 06

  45. Using PHP to Append Data to Files MileageLog requirement: Write a program that allows a small business owner to submit travel mileage for a business trip. The program should receive the mileage and append this to a text file named mileageLog.txt, then inform the user that the data has been added. The user can use the form to submit mileage as often as needed. Chapter 06

  46. Algorithm for mileageLog.html mileageLog.html algorithm: Prompt for mileage Get mileage Submit mileage to mileageLog.php END Chapter 06

  47. Code for mileageLog.html <body> <h1>Mileage Log</h1> <form action = "mileageLog.php" method = "post" > <p>Enter your mileage: <input type = "text" size = "5" name = "mileage" /></p> <p><input type="submit" value="Submit mileage" /></p> </form> </body> Chapter 06

  48. Algorithm for mileageLog.php mileageLog.php algorithm: Receive mileage from mileageLog.html   Open mileageLog.txt as logFile for appending Write mileage to logFile Close logFile   Display "Mileage has been recorded" message to the user END Chapter 06

  49. Code for mileageLog.php <?php $ mileage = $_POST['mileage']; $logFile =fopen("mileageLog.txt","a"); fputs($logFile, "$mileage\n"); fclose($logFile ); print (" <h1>Your mileage submission ($mileage) has been recorded:</h1>"); print (" <p><a href = \"mileageLog.html\">Submit another mileage</a></p> "); ?> Chapter 06

  50. Code for mileageLog.php $logFile =fopen("mileageLog.txt","a"); fputs($logFile, "$mileage\n"); fclose($surveyFile); The file is opened for append operations ("a") The fputs functions is used to write the mileage to the file. A new line marker \n is added at the end to start a new line, ready for the next mileage to be appended. The file is closed Chapter 06