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

Phát triển web với PHP và MySQL - p 77 ppt

10 205 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 10
Dung lượng 533,4 KB

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

Nội dung

Second, you will see that for a reply, the text of the original message is inserted and marked with a “>” character as is the case in most mail and news reading programs.. Third, you can

Trang 1

First, look at the URL:

http://webserver/chapter29/new_post.php?parent=5

The parameter passed in as parentwill be the parent postidof the new posting If you click

New Post instead of Reply, you will get parent=0in the URL

Second, you will see that for a reply, the text of the original message is inserted and marked

with a “>” character as is the case in most mail and news reading programs

Third, you can see that the title of this message defaults to the title of the original message

pre-fixed with “Re:”

Let’s look at the code that produces this output It is shown in Listing 29.8

LISTING 29.8 new_post.php—Allows a User to Type a New Post or Reply to an Existing Post

<?

include (‘include_fns.php’);

if(!$area)

$area = 1;

if(!$error)

{

if(!$parent) {

$parent = 0;

if(!$title)

$title = “New Post”;

} else { // get post name

$title = get_post_title($parent);

// append Re:

if(strstr($title, “Re: “) == false )

$title = “Re: “.$title;

//make sure title will still fit in db

$title = substr($title, 0, 20);

//prepend a quoting pattern to the post you are replying to

$message = add_quoting(get_post_message($parent));

} }

29

Trang 2

display_new_post_form($parent, $area, $title, $message, $name);

if($error) echo “Your message was not stored Make sure you have filled in all fields and try again.”;

do_html_footer();

?>

After some initial setting up, this script checks whether the parent is zero or otherwise If it is zero, this is a new topic, and little further work is needed

If this is a reply ($parentis the postidof an existing article), then the script goes ahead and sets up the title and the text of the original message, as follows:

// get post name

$title = get_post_title($parent);

// append Re:

if(strstr($title, “Re: “) == false )

$title = “Re: “.$title;

//make sure title will still fit in db

$title = substr($title, 0, 20);

//prepend a quoting pattern to the post you are replying to

$message = add_quoting(get_post_message($parent));

The functions it uses here are get_post_title(),get_post_message(), and add_quoting() These functions are all from the discussion_fns.phplibrary They are shown in Listings 29.9, 29.10, and 29.11, respectively

L ISTING 29.9 get_post_title() Function from discussion_fns.php—Retrieves a Message’s Title from the Database

function get_post_title($postid) {

// extract one post’s name from the database if(!$postid) return “”;

$conn = db_connect();

L ISTING 29.8 Continued

Trang 3

//get all header information from ‘header’

$query = “select title from header where postid = $postid”;

$result = mysql_query($query);

if(mysql_numrows($result)!=1)

return “”;

return mysql_result($result, 0, 0);

}

LISTING 29.10 get_post_message() Function from discussion_fns.php—Retrieves a

Message’s Body from the Database

function get_post_message($postid)

{

// extract one post’s message from the database

if(!$postid) return “”;

$conn = db_connect();

$query = “select message from body where postid = $postid”;

$result = mysql_query($query);

if(mysql_numrows($result)>0)

{

return mysql_result($result,0,0);

}

}

These first two functions retrieve an article’s header and body (respectively) from the database

LISTING 29.11 add_quoting() Function from discussion_fns.php—Indents a Message Text

with “>” Symbols

function add_quoting($string, $pattern = “> “)

{

// add a quoting pattern to mark text quoted in your reply

return $pattern.str_replace(“\n”, “\n$pattern”, $string);

}

Theadd_quoting()function reformats the string to begin each line of the original text with a

symbol, which defaults to >

29

L ISTING 29.9 Continued

Trang 4

After the user types in his reply and clicks the Post button, he will be taken to the

store_new_post.phpscript Sample output from this script is shown in Figure 29.9

F IGURE 29.9

The new post is now visible in the tree.

The new post is there in the figure, under Re: using gd? - Laura - 08:28 09/26/2000 Other than that, this page looks like the regular index.phppage

Let’s look at the code for store_new_post.php It is shown in Listing 29.12

L ISTING 29.12 store_new_post.php—Puts the New Post in the Database

<?

include (“include_fns.php”);

if($id = store_new_post($HTTP_POST_VARS)) {

include (“index.php”);

} else {

$error = true;

include (“new_post.php”);

}

?>

Trang 5

As you can see, this is a short script Its main task is to call the store_new_post()function.

This page has no visual content of its own If storing succeeds, we see the index page

Otherwise, we go back to the new_post.phppage, so the user can try again

The store_new_post()function is shown in Listing 29.13

LISTING 29.13 store_new_post() Function from discussion_fns.php—Validates and Stores

the New Post in the Database

function store_new_post($post)

{

// validate clean and store a new post

$conn = db_connect();

// check no fields are blank

if(!filled_out($post))

return false;

$post = clean_all($post);

//check parent exists

if($post[“parent”]!=0)

{

$query = “select postid from header where postid = ‘“.$post[‘parent’].”’”;

$result = mysql_query($query);

if(mysql_numrows($result)!=1) {

return false;

} }

// 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);

29

Trang 6

$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

where parent = ‘“.$post[“parent”].”’

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) {

L ISTING 29.13 Continued

Trang 7

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:

• You could add navigation to the view options, so that from a post you could navigate to the next message, the previous message, the next-in-thread message, or the previous-in-thread message

• You could add an administration interface for setting up new forums and deleting old posts

• You could add user authentication so only registered users could post

• You could add some kind of moderation or censorship mechanism

Look at existing systems for ideas

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

avail-able You can get it from

http://www.phpslash.org

29

L ISTING 29.13 Continued

Trang 8

In Chapter 30, “Generating Personalized Documents in Portable Document Format (PDF),” we will use the PDF format to deliver documents that are attractive, print consistently and are somewhat tamperproof This is useful for a range of service-based applications, such as gener-ating contracts online

Trang 9

30

Generating Personalized

Documents in Portable

Document Format (PDF)

Trang 10

On service driven sites, we sometimes need to deliver personalized documents, generated in response to input from our visitors This can be used to provide an automatically 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

• How to use PHP string processing to integrate a template with a user’s data to create a Rich Text Format (RTF) document

• How to use a similar approach to generate a Portable Document Format (PDF) document

• 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, consisting 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 certificate 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 compro-mise when choosing a delivery format to meet as many of these ten attributes as possible

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

w