PHP 5/MySQL Programming- P47 ppt

5 202 0
PHP 5/MySQL Programming- P47 ppt

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

Thông tin tài liệu

</center></td> </tr> </table> </form> HERE; } // end showTest Although the code is long, almost all of it is pure HTML. The PHP part selects HTML files and places them in the select group. This code fragment uses the preg_grep() to select filenames ending in HTML and creates an option tag for that file. Note that I stripped out the .html part of the filename because I won’t need it. It would complicate some of the code coming up in the takeQuiz program. Showing the Edit List The showEdit() function works a lot like showTest(), listing the system’s master files. Although it is often exactly the same as the list of tests, it won’t always be the same; some master files may not have been made into HTML files. function showEdit(){ // let user select a master file to edit global $theFiles; //get only quiz master files $testFiles = preg_grep(“/mas$/”, $theFiles); //edit a quiz print <<<HERE <form action = “editQuiz.php” method = “post”> <table border = 1 width = 400> <tr> <td colspan = 2><center> <h3>Edit a quiz</h3> </center></td> </tr> 208 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 <tr> <td>Administrative Password</td> <td> <input type = “password” name = “password” value = “”> </td> </tr> <tr> <td>Quiz</td> <td> <select name = “editFile”> <option value = “new”>new quiz</option> HERE; foreach ($testFiles as $myFile){ $fileBase = substr($myFile, 0, strlen($myFile) - 4); print “ <option value = $myFile>$fileBase</option>\n”; } // end foreach print <<<HERE </select> </td> </tr> <tr> <td colspan = 2><center> <input type = “submit” value = “go”> </center></td> </tr> </table> </form> HERE; } // end showEdit 209 C h a p t e r 6 W o r k i n g w i t h F i l e s The showEdit() function is just like showQuiz() but the form points to the editQuiz.php program and the file list is based on those files ending in mas. There’s one other subtle but important difference: Look at the code for the select element and see a new quiz option. If the user chooses this option, the editQuiz() function won’t try to load a quiz file into memory, but sets up for a new quiz instead. Showing the Log List The last segment is meant for the quiz administrator. It allows the user with administrator access to view the log of any system quiz. This log shows who has taken the test, where and when she took it, and her score. When the user clicks the submit button associated with this part of the page, the showLog.php program takes over. function showLog(){ //let user choose from a list of log files global $theFiles; print <<<HERE <form action = “showLog.php” method = “post”> <table border = 1 width = 400> <tr> <td colspan = 2><center> <h3>Show a log file</h3> </td> </tr> <tr> <td>Administrative Password</td> <td> <input type = “password” name = “password” value = “”> </td> </tr> 210 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 211 C h a p t e r 6 W o r k i n g w i t h F i l e s <tr> <td>Quiz</td> <td> <select name = “logFile”> HERE; //select only log files $logFiles = preg_grep(“/log$/”, $theFiles); foreach ($logFiles as $myFile){ $fileBase = substr($myFile, 0, strlen($myFile) - 4); print “ <option value = $myFile>$fileBase</option>\n”; } // end foreach print <<<HERE </select> </td> </tr> <tr> <td colspan = 2><center> <input type = “submit” value = “go”> </td> </tr> </table> </form> HERE; } // end showLog ?> </center> </body> </html> I decided that all log files would end with .log, so the program can easily get a list of log files to place in the select group. 212 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 Editing a Test For simplicity’s sake I decided on a very simple test format. The first three lines of the test file contain the test’s name, the instructor’s e-mail address, and the test’s password. The test data itself follows. Each problem takes up one line (although it can wrap freely—a line is defined by a carriage return character). The problem has a question followed by four possible answers and the correct answer. A colon separates each element. The editQuiz.php program assists the user in creating and editing quizzes. It’s a simple program, because the real work happens after the user edits and presses the submit button. Getting Existing Test Data The first chore of the editQuiz program is determining which quiz the user is requesting. Remember that the value new indicates that the user wants to build a new test; that value is treated specially. Any other value is the foundation of a test filename, so I open the appropriate master file and load its values into the appropriate form elements: <html> <head> <title>Quiz Builder</title> </head> <body> <? if ($password != “absolute”){ print <<<HERE <font color = “red” size = +3> IN THE REAL WORLD You think question formatting has too many rules? I agree. This is a limitation of the sequential-file access technique that’s storing the data. In chapters 8-12, you learn ways that aren’t quite so picky. However, this is a relatively easy way to store your data, so I wrote the program to assist the process as much as is practical. You generally want to write your program so the user never has to know the underlying data structure. . page, the showLog .php program takes over. function showLog(){ //let user choose from a list of log files global $theFiles; print <<<HERE <form action = “showLog .php method = “post”> <table. preg_grep(“/mas$/”, $theFiles); //edit a quiz print <<<HERE <form action = “editQuiz .php method = “post”> <table border = 1 width = 400> <tr> <td colspan = 2><center> <h3>Edit. showEdit 209 C h a p t e r 6 W o r k i n g w i t h F i l e s The showEdit() function is just like showQuiz() but the form points to the editQuiz .php program and the file list is based on those files ending in mas. There’s one other subtle but

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

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Contents

    • Introduction

    • 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 11: Data Normalization

    • Chapter 12: Building a Three-Tiered Data Application

    • Index

    • Team DDU

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

Tài liệu liên quan