1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P50 pot

5 254 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 126,7 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

//the first three lines are name, instructor’s email, and password$quizName = fgets$msfp; $quizEmail = fgets$msfp; $quizPwd = fgets$msfp; The master file is opened with read access.. Cre

Trang 1

//the first three lines are name, instructor’s email, and password

$quizName = fgets($msfp);

$quizEmail = fgets($msfp);

$quizPwd = fgets($msfp);

The master file is opened with read access The first three lines are unimportant,

but I must read them to get to the quiz data

Creating an Answer Key

I start by generating an answer key from the master file, stepping through each

question in the file and extracting all the normal variables from it (although I’m

interested only in the $correctvariable) I then store the $correctvalue in an

array called $key At the end of this loop, the $keyarray holds the correct answer

for each quiz question

//step through the questions building an answer key

$numCorrect = 0;

$questionNumber = 1;

while (!feof($msfp)){

$currentProblem = fgets($msfp);

list($question, $answerA, $answerB, $answerC, $answerD, $correct) =

split (“:”, $currentProblem);

$key[$questionNumber] = $correct;

$questionNumber++;

} // end while

fclose($msfp);

Checking the User’s Response

The user’s responses come from the HTML form as an array called $quest The

cor-rect answers are in an array called $key To grade the test, I step through both

arrays at the same time, comparing the user’s response with the correct

response Each time these values are the same, the user has gotten an answer

cor-rect The user was incorrect when the values are different or there was a problem

with the test itself; don’t discount that as a possibility Unfortunately, you can’t

do much about that, because the test author is responsible for making sure the

test is correct Still, you might be able to improve the situation somewhat by

pro-viding a better editor that ensures the test is in the right format and each

ques-tion has an answer registered with it

223

Trang 2

//Check each answer from user

for ($questionNumber = 1; $questionNumber <= count($quest);

$questionNumber++){

$guess = $quest[$questionNumber];

$correct = $key[$questionNumber];

$correct = rtrim($correct);

if ($guess == $correct){

//user got it right

$numCorrect++;

print “problem # $questionNumber was correct<br>\n”;

} else {

print “<font color = red>problem # $questionNumber was incorrect</font><br>\n”;

} // end if

} // end for

I give a certain amount of feedback, telling whether the question was correct, but

I decide not to display the right answer You might give the user more or less information, depending on how you’re using the quiz program

Reporting the Results to Screen and Log File

After checking each answer, the program reports the results to the user as a raw score and a percentage The program also opens a log file for append access and adds the current data to it Append access is just like write access, but rather than overwriting an existing file, it adds any new data to the end of it

print “you got $numCorrect right<br>\n”;

$percentage = ($numCorrect /count($quest)) * 100;

print “for $percentage percent<br>\n”;

$today = date (“F j, Y, g:i a”);

//print “Date: $today<br>\n”;

$location = getenv(“REMOTE_ADDR”);

//print “Location: $location<br>\n”;

//add results to log file

$lgfp = fopen($logFile, “a”);

$logLine = $student “\t”;

$logLine = $today “\t”;

$logLine = $location “\t”;

$logLine = $numCorrect “\t”;

224

g r

s o

l u

g in

e r

Trang 3

$logLine = $percentage “\n”;

fputs($lgfp, $logLine);

fclose($lgfp);

?>

</html>

I add a few more elements to the log file that might be useful to a test

adminis-trator Of course, I add the student’s name and current date I also added a

loca-tion variable, which uses the $REMOTE_ADDR environment variable to indicate

which machine the user was on when she submitted the exam This can be

use-ful because it can alert you to certain kinds of hacking (A person taking the same

quiz several times on the same machine but with a different name, for example.)

The gradeQuizprogram adds the number correct and the percentage to the log

file as well, then closes the file

Notice that the data in the log file is delimited with tab characters This is done

so an analysis program could easily work with the file using the splitcommand

Also, most spreadsheet programs can read a tab-delimited file, so the log file is

easily imported into a spreadsheet for further analysis

Look at the PHP online Help for the date functions to see all the ways you can dis-play the current date.

You can really improve the logging functionality if you want to do some in-depth test analysis For example, store each user’s response to each question in the quiz.

This gives you a database of performance on every question, so you could easily determine which questions are causing difficulty.

Viewing the Log

The showLog.php program is actually very similar to the takeQuiz program It

checks the password to ensure the user has administrator access, then opens the

log using the file()function It prints the file results inside a <pre></pre>pair,

so the tab characters are preserved

<?

//showLog.php

//shows a log file

T R I C K

T R I C K

225

Trang 4

//requires admin password

if ($password == “absolute”){

$lines = file($logFile);

print “<pre>\n”;

foreach ($lines as $theLine){

print $theLine;

} // end foreach

print “</pre>\n”;

} else {

print <<<HERE

<font color = “red”

size = +2>

You must have the appropriate password to view this log

</font>

HERE;

} // end if

?>

Improve this program by writing the data into an HTML table However, not all spreadsheets can easily work with HTML table data, so I prefer the tab format It isn’t difficult to add data analysis to the log viewer, including mean scores, stan-dard deviation, and suggested curve values

Summary

This chapter explores the use of sequential files as a data storage and retrieval mechanism You learned how to open files in read, write, and append modes and you know how file pointers refer to a file You wrote data to a file and loaded data from a file with appropriate functions You learned how to load an entire file into an array You can examine a directory and determine which files are in the directory You learned how to use basic regular expressions in the preg_greq() function to display a subset of files in the directory Finally, you put all this together in a multi-program system that allows multiple levels of access to an interesting data set You should be proud

226

g r

s o

l u

g in

e r

Trang 5

C H A L L E N G E S

1 Improve the quiz program in one of the ways I suggested throughout the

chapter: Add the ability to e-mail test results, put in some test scores analysis,

improve the quiz editing page, or try something of your own.

2 A couple of values in this system should be global among each of the PHP

programs The root directory of the files and the administrative password

are obvious candidates Write a program that stores these values in an ini

file and modify the quiz programs to get these values from that file when

needed.

3 Create a source code viewer Given a filename, the program should read in

the file and convert each instance of < into &lt; Save this new file to

another name This allows you to show your source code to others.

4 Create a simple guest book Let the user enter information into a form, and

add her comment to the bottom of the page when she clicks the submit

button You can use one or two files for this.

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

TỪ KHÓA LIÊN QUAN