PHP and MySQL Web Development - P149 ppsx

5 78 0
PHP and MySQL Web Development - P149 ppsx

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

Thông tin tài liệu

712 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF) Software to Create PDF Programmatically Support for creating PDF documents is available from within PHP.Two different func- tion libraries are available, with similar intentions. As they rely on external libraries, nei- ther is compiled in to PHP by default. PHP’s PDFlib functions use the PDFlib library, available from http://www.pdflib.com The ClibPDF functions use the ClibPDF library, available from http://www.fastio.com/ Both these libraries are similar.They provide an API of functions to generate a PDF doc- ument.We have elected to use PDFlib because it seems to be updated and maintained more regularly. It is worth noting that neither of these libraries are Free Software. Both permit some non-commercial use without charge, but require a license fee if you intend to provide a commercial service using them. You can see if PDFlib is already installed on your system by checking the output of the function phpinfo(). Under the heading pdf, you can find out if PDFlib support is enabled, as well as the version of PDFlib used. If you intend to use TIFF or JPEG images in your PDF documents, you will also need to install the TIFF library, available from http://www.libtiff.org/ and the JPEG library, available from ftp://ftp.uu.net/graphics/jpeg/ On a Unix system, these pieces of software are installed in the usual way, using configure and make.You will need to recompile PHP with the switch with-pdflib. On a Windows server, the PDFlib DLL is bundled in the PHP Zip file, so you will just need to uncomment the extension in your php.ini file. Solution Overview We will produce a system with three possible outcomes. As shown in Figure 30.1, we will ask quiz questions, assess the answers, and then generate a certificate in one of three ways: n We will generate an RTF document from a blank template. n We will generate a PDF document from a blank template. n We will generate a PDF document programmatically via PDFlib. 36 525x ch30 1/24/03 3:40 PM Page 712 713 Solution Overview Figure 30.1 Our certification system will generate one of three different certificates. A summary of the files in the certification project is shown in Table 30.1. Table 30.1 Files in the Certification Application Name Type Description index.html HTML page The HTML form that contains the quiz ques- tions score.php Application Script to assess users’ answers rtf.php Application Script to generate RTF certificate from tem- plate pdf.php Application Script to generate PDF certificate from tem- plate pdflib.php Application Script to generate PDF certificate using PDFlib signature.png image Bitmap image of signature to be included on the PDFlib certificate PHPCertification.rtf RTFRTF certificate template PHPCertification.pdf PDF PDF certificate template Let’s go ahead and look at the application. Asking the Questions The file index.html is straightforward. It needs to contain an HTML form asking the user for his name, and the answer to a number of questions. In a real assessment applica- tion, we would most likely retrieve these questions from a database. Here we are focusing on producing the certificate, so we will just hard-code some questions into the HTML. Generate RTF file from blank template Generate RTF file from blank template Generate PDF via PDFlib Assess Quiz Answers Ask Quiz Questions 36 525x ch30 1/24/03 3:40 PM Page 713 714 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF) The name field is a text input. Each question has three radio buttons to allow the user to indicate his preferred answer.The form has an image button as a submit button. The code for this page is shown in Listing 30.1. Listing 30.1 index.html—HTML Page Containing Quiz Questions <html> <body> <h1><p align="center"> <img src="rosette.gif" alt=""> Certification <img src="rosette.gif" alt=""></p></h1> <p>You too can earn your highly respected PHP certification from the world famous Fictional Institute of PHP Certification.</p> <p>Simply answer the questions below:</p> <form action="score.php" method="post"> <p>Your Name <input type="text" name="name"></p> <p>What does the PHP statement echo do?</p> <ol> <li><input type="radio" name="q1" value="1"> Outputs strings.</li> <li><input type="radio" name="q1" value="2"> Adds two numbers together.</li> <li><input type="radio" name="q1" value="3"> Creates a magical elf to finish writing your code.</li> </ol> <p>What does the PHP function cos() do?</p> <ol> <li><input type="radio" name="q2" value="1"> Calculates a cosine in radians.</li> <li><input type="radio" name="q2" value="2"> Calculates a tangent in radians. </li> <li><input type="radio" name="q2" value="3"> It is not a PHP function it is a lettuce. </li> </ol> <p>What does the PHP function mail() do?</p> <ol> <li><input type="radio" name="q3" value="1"> Sends a mail message. <li><input type="radio" name="q3" value="2"> Checks for new mail. <li><input type="radio" name="q3" value="3"> 36 525x ch30 1/24/03 3:40 PM Page 714 715 Solution Overview Toggles PHP between male and female mode. </ol> <p align="center"><input type="image" src="certify-me.gif" border="0"></p> </form> </body> </html> The result of loading index.html in a Web browser is shown in Figure 30.2. Listing 30.1 Continued Figure 30.2 index.html asks the user to answer quiz questions. Grading the Answers When the user submits his answers to the questions in index.html,we need to grade him and calculate a score.This is done by the script called score.php.The code for this script is shown in Listing 30.2. 36 525x ch30 1/24/03 3:40 PM Page 715 716 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF) Listing 30.2 score.php—Script to Mark Exams <?php //create short variable names $q1 = $HTTP_POST_VARS['q1']; $q2 = $HTTP_POST_VARS['q2']; $q3 = $HTTP_POST_VARS['q3']; $name = $HTTP_POST_VARS['name']; // check that all the data was received if($q1==''||$q2==''||$q3==''||$name=='') { echo '<h1><p align = center><img src="rosette.gif" alt=""> Sorry: <img src="rosette.gif" alt=""></p></h1>'; echo '<p>You need to fill in your name and answer all questions</p>'; } else { //add up the scores $score = 0; if($q1 == 1) // the correct answer for q1 is 1 $score++; if($q2 == 1) // the correct answer for q2 is 1 $score++; if($q3 == 1) // the correct answer for q3 is 1 $score++; //convert score to a percentage $score = $score / 3 * 100; if($score < 50) { // this person failed echo '<h1 align="center"><img src="rosette.gif" alt="" /> Sorry: <img src="rosette.gif" alt="" /></h1>'; echo '<p>You need to score at least 50% to pass the exam</p>'; } else { // create a string containing the score to one decimal place $score = number_format($score, 1); echo '<h1 align="center"><img src="rosette.gif" alt="" /> 36 525x ch30 1/24/03 3:40 PM Page 716 . available from within PHP. Two different func- tion libraries are available, with similar intentions. As they rely on external libraries, nei- ther is compiled in to PHP by default. PHP s PDFlib functions. that contains the quiz ques- tions score .php Application Script to assess users’ answers rtf .php Application Script to generate RTF certificate from tem- plate pdf .php Application Script to generate. need to recompile PHP with the switch with-pdflib. On a Windows server, the PDFlib DLL is bundled in the PHP Zip file, so you will just need to uncomment the extension in your php. ini file. Solution

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

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

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

Tài liệu liên quan