PHP 5/MySQL Programming- P46 potx

5 258 0
PHP 5/MySQL Programming- P46 potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

203 C h a p t e r 6 W o r k i n g w i t h F i l e s element of the array. This is especially useful when your text file contains data, because each line in my data file represents one individual’s data. The file() command is so easy you might be tempted to use it all the time. The command loads the entire file into memory, so you should only use it for relatively small files. When you use the fgets() technique, you only need one line from the file in memory at a time, so the fgets() method can be effectively used on any size file without affecting performance. Using file() on a very large file can be extremely slow. Splitting a Line into an Array and to Scalar Values You might recall the split() function from chapter 5, “Better Arrays and String Handling.” This function separates string elements based on some delimiter. I use the split() function inside a foreach loop to break each line into its con- stituent values. However, I really don’t want an array in this situation. Instead, I want the first value on the line to be read into the $name variable, and the second value stored in $email. The list() function allows you to take an array and distribute its con- tents into scalar (non-array) variables. In this particular situation, I never stored the results of the split() function in an array at all, but immediately listed the contents into the appropriate scalar variables. Once the data is in the variables, you can easily interpolate it into a mail-merge message. The next obvious step for this program is to automatically send each message as an e-mail. PHP provides a function called mail(), which makes it quite easy to add this functionality. However, the function is dependent on how the server is set up and doesn’t work with equal reliability on every server. Also, there are good and not-so-good reasons to send e-mail through a program. It’s completely legitimate to send e-mails to people when they request it or to have a program send you e-mails when certain things happen. For example, my own more secure version of the tester program sends an e-mail when conditions indi- cate potential cheating. A program that sends unsolicited e-mail to people is rude and causes bad feelings about your site. Creating the QuizMachine.php Program The quiz tool from the beginning of this chapter is an entire system of programs designed to work together—in this case, five different programs. Each quiz is stored in two separate files, which the programs automatically generate. Figure 6.11 illustrates how the various programs fit together. TRAP TRAP The QuizMachine.php program is the entry point to the system for both the test administrator and the quiz taker. The program essentially consists of three forms that allow access to the other parts of the program. To ensure a minimal level of security, all other programs in the system require password access. The QuizMachine.php program primarily serves as a gateway to the other parts of the system. If the user has administrative access (determined by a password), he can select an exam and call the editQuiz.php page. This page loads the quiz’s actual master file (if it already exists) or sets up a prototype quiz and places the quiz data in a Web page as a simple editor. The editQuiz program calls the writeQuiz.php program, which takes the results of the editQuiz form and writes it to a master test file and an HTML page. If the user wants to take a quiz, the system moves to the takeQuiz.php page, which checks the user’s password and presents the quiz if authorized. When the user indicates he is finished, the gradeQuiz.php program grades the quiz and stores the result in a text file. Finally, the administrator can examine the log files resulting from any of the quizzes by indicating a quiz from the quizMachine page. The showLog.php program displays the appropriate log file. Building the QuizMachine.php Control Page The heart of the quiz system is the QuizMachine.php page. The user directly enters this page only. All the other parts are called from this page or from one of the pages it calls. This page acts as a control panel. It consists of three parts, which correspond to the three primary jobs this system can do: writing or editing 204 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 6.11 This diagram illustrates a user’s movement through the Quiz Machine system. quizzes, taking quizzes, and analyzing quiz results. In each of these cases, the user has a particular quiz in mind, so the control panel automatically provides a list of appropriate files in each segment. Also, each of these tasks requires a password. The main part of the QuizMachine.php program simply sets up the opening HTML and calls a series of functions, which do all the real work: <html> <head> <title>Quiz Machine</title> </head> <body> <center> <h1>Quiz Machine</h1> <? getFiles(); showTest(); showEdit(); showLog(); The program calls getFiles() first. This function examines a directory and gets a list of the files in that directory. This list of filenames is used in the other func- tions. The next three functions generate HTML forms. Each form contains a select list that is dynamically generated from the file list. The button corresponding to each form submits the form to the appropriate PHP page. Make another version of this main page for the people who will take your test. On the new page, you don’t even show the administrative options. It’s very easy to make such a page. Simply copy the QuizMachine.php program to another file and remove the calls to the showEdit() and showLog() functions. Getting the File List Since most of the code in the QuizMachine program works with a list of files, the getFiles() function shown below is charged with that important task. function getFiles(){ //get list of all files for use in other routines global $dirPtr, $theFiles; TRICK 205 C h a p t e r 6 W o r k i n g w i t h F i l e s chdir(“.”); $dirPtr = openDir(“.”); $currentFile = readDir($dirPtr); while ($currentFile !== false){ $theFiles[] = $currentFile; $currentFile = readDir($dirPtr); } // end while } // end getFiles The first thing this function does is change the file system so it points at the cur- rent directory, then the program sets up a pointer variable to that directory. The directory that holds the PHP programs is open for anybody to see. You might not want your test files to be so conspicuous. To simplify this example, I kept all the test files in the same directory as the program itself, but you can keep the data files in a different directory. You might store all the data files in a part of your directory that is unavailable to the Web (away from your public_html structure, for instance) so that people can’t see the answer key by browsing to it. If you do this, change each directory reference throughout the system. I then created an array called theFiles, which holds every filename in the direc- tory. The theFiles variable is global, so it is shared with the program and other functions that declare a reference to it. Showing the Take a Test List Most of your users don’t create or edit quizzes. Instead, they take them. To take a test, the user must choose a test and enter the password associated with it. To simplify choosing a test, the showTest() function grabs all the HTML files in the quiz directory and places them in a select list. The password goes in an ordinary password field. The code in showTest() creates a form that calls the takeQuiz.php program when it is submitted: function showTest(){ //print a list of tests for user to take global $theFiles; print <<<HERE <form action = “takeQuiz.php” method = “post”> TRAP 206 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <table border = 1 width = 400> <tr> <td colspan = 2><center> <h3>Take a quiz</h3> </td> </tr> <tr> <td>Quiz Password</td> <td> <input type = “password” name = “password”> </td> </tr> <tr> <td>Quiz</td> <td> <select name = “takeFile”> HERE; //select only quiz html files $testFiles = preg_grep(“/html$/”, $theFiles); foreach ($testFiles as $myFile){ $fileBase = substr($myFile, 0, strlen($myFile) - 5); print “ <option value = $fileBase>$fileBase</option>\n”; } // end foreach print <<<HERE </select> </td> </tr> <tr> <td colspan = 2><center> <input type = “submit” value = “go”> 207 C h a p t e r 6 W o r k i n g w i t h F i l e s . quizMachine page. The showLog .php program displays the appropriate log file. Building the QuizMachine .php Control Page The heart of the quiz system is the QuizMachine .php page. The user directly. the writeQuiz .php program, which takes the results of the editQuiz form and writes it to a master test file and an HTML page. If the user wants to take a quiz, the system moves to the takeQuiz .php page,. QuizMachine .php program primarily serves as a gateway to the other parts of the system. If the user has administrative access (determined by a password), he can select an exam and call the editQuiz.php

Ngày đăng: 07/07/2014, 02:20

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 12: Building a Three-Tiered Data Application

Tài liệu cùng người dùng

Tài liệu liên quan