This is especially useful when your text file contains data, because each line in my data file represents one individual’s data.. The command loads the entire file into memory, so you sh
Trang 1element 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 foreachloop 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 $namevariable, and the second value stored
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
T R A P
T R A P
Trang 2The QuizMachine.phpprogram 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 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 editQuizprogram calls the
it to a master test file and an HTML page
If the user wants to take a quiz, the system moves to the takeQuiz.phppage, 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 quizMachinepage The showLog.phpprogram 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
g r
s o
l u
g in
e r
FIGURE 6.11
This diagram
illustrates a user’s
movement through
the Quiz
Machine system.
Trang 3quizzes, 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.phpprogram 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 QuizMachineprogram works with a list of files, the
function getFiles(){
//get list of all files for use in other routines
global $dirPtr, $theFiles;
T R I C K
205
Trang 4$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 theFilesvariable 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 quizdirectory and places them in a select list The password goes in an ordinary
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”>
T R A P
206
g r
s o
l u
g in
e r
Trang 5<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