Although the original software is written in Perl, this PHP version is available.You can get it from http://www.phpslash.org Next In Chapter 30, “Generating Personalized Documents in Por
Trang 1702 Chapter 29 Building Web Forums
// check not a duplicate
$query = "select header.postid from header, body where
header.postid = body.postid and header.parent = ".$post['parent']." and header.poster = '".$post['poster']."' and header.title = '".$post['title']."' and header.area = ".$post['area']." and body.message = '".$post['message']."'";
$result = mysql_query($query);
if (!$result) {
return false;
} if(mysql_numrows($result)>0) return mysql_result($result, 0, 0);
$query = "insert into header values
('".$post['parent']."', '".$post['poster']."', '".$post['title']."', 0,
'".$post['area']."', now(),
NULL )";
$result = mysql_query($query);
if (!$result) {
return false;
} // note that our parent now has a child
$query = 'update header set children = 1 where postid = '.$post['parent'];
$result = mysql_query($query);
if (!$result) {
return false;
} // find our post id, note that there could be multiple headers // that are the same except for id and probably posted time
$query = "select header.postid from header left join body
on header.postid = body.postid
Listing 29.13 Continued
Trang 2703 Extensions
and poster = '".$post['poster']."' and title = '".$post['title']."' and body.postid is NULL";
$result = mysql_query($query);
if (!$result) {
return false;
} if(mysql_numrows($result)>0)
$id = mysql_result($result, 0, 0);
if($id) {
$query = "insert into body values ($id, '".$post['message']."')";
$result = mysql_query($query);
if (!$result) {
return false;
} return $id;
} }
This is a long function, but it is not overly complex It is only long because inserting a posting means inserting entries in the header and body tables, and updating the parent article’s row in the header table to show that it now has children
That is the end of the code for the Web forum application
Extensions
There are many extensions you could add to this project:
n You could add navigation to the view options, so that from a post you could navi-gate to the next message, the previous message, the next-in-thread message, or the previous-in-thread message
n You could add an administration interface for setting up new forums and deleting old posts
n You could add user authentication so only registered users could post
n You could add some kind of moderation or censorship mechanism
Look at existing systems for ideas
Listing 29.13 Continued
Trang 3704 Chapter 29 Building Web Forums
Using an Existing System
There are a couple of noteworthy existing systems
Phorum is an Open Source Web forums project It has different navigation and semantics from ours, but its structure is relatively easily customized to fit into your own site A notable feature of phorum is that it can be configured by the actual user to display
in either a threaded or flat view.You can find out more about it at
http://www.phorum.org
Another interesting project is phpslash.This is a port of the software used to run the Slashdot discussion boards Although the original software is written in Perl, this PHP version is available.You can get it from
http://www.phpslash.org
Next
In Chapter 30, “Generating Personalized Documents in Portable Document Format (PDF),” we will use the PDF format to deliver documents that are attractive, print con-sistently, and are somewhat tamperproof.This is useful for a range of service-based appli-cations, such as generating contracts online
Trang 4Generating Personalized Documents in Portable Document Format (PDF)
ON SERVICE DRIVEN SITES,WE SOMETIMES NEEDto deliver personalized documents, generated in response to input from our visitors.This can be used to provide an auto-matically filled in form or to generate personalized documents, such as legal documents, letters, or certificates
Our example in this chapter will present a user with an online skill assessment page and generate a certificate
We will explain
n How to use PHP string processing to integrate a template with a user’s data to create a Rich Text Format (RTF) document
n How to use a similar approach to generate a Portable Document Format (PDF) document
n How to use PHP’s PDFlib functions to generate a similar PDF document
The Problem
We want to be able to give our visitors an exam consisting of a number of questions If they answer enough of the questions correctly, we will generate a certificate for them to show that they have passed the exam
So that a computer can mark them easily, our questions will be multiple choice, con-sisting of a question and a number of potential answers Only one of the potential answers for each question will be correct
If a user achieves a passing grade on the questions, he will be presented with a certifi-cate
Trang 5706 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF)
Ideally, the file format for our certificate should
1 Be easy to design
2 Be able to contain a variety of different elements such as bitmap and vector images
3 Result in a high quality printout
4 Only require a small file to be downloaded
5 Be generated almost instantly
6 Be at a low cost to produce
7 Work on many operating systems
8 Be difficult to fraudulently duplicate or modify
9 Not require any special software to view or print
10 Display and print consistently for all recipients Like many decisions we need to make from time to time, we will probably need to compromise when choosing a delivery format to meet as many of these ten attributes as possible
Evaluating Document Formats
The most important decision we need to make is what format to deliver the certificate
in Options include paper, ASCII text, HTML, Microsoft Word, or another word proces-sor’s format, Rich Text Format, PostScript, and Portable Document Format Given the ten attributes listed previously, we can consider and compare some of our options
Paper
Delivering the certificate on paper has some obvious advantages.We retain complete control over the process.We can see exactly what each certificate output looks like before sending it to the recipient.We do not need to worry about software or band-width, and the certificate could be printed with anti-counterfeiting measures
It would meet all of our needs except for attributes 5 and 6.The certificate could not
be created and delivered quickly Postal delivery could take days or weeks depending on our and the recipient’s location
Each certificate would also cost us a few cents to a few dollars in printing and postage costs and probably more in handling Automatic electronic delivery would be cheaper
ASCII
Delivering documents as ASCII or plain text comes with some advantages Compatibility will be no problem Bandwidth required would be small, so cost would be very low.The simplicity of the end result will make it very easy to design and very quick for a script to generate