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

PHP and MySQL Web Development - P146 pdf

5 202 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 148,31 KB

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

Nội dung

Figure 29.8 Replies have the text of the original automatically inserted and marked.. Listing 29.8 new_post.php—Allows a User to Type a New Post or Reply to an Existing Post... If this

Trang 1

Figure 29.8 Replies have the text of the original automatically

inserted and marked

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

Existing Post

<?php include ('include_fns.php');

$title = $HTTP_POST_VARS['title'];

$poster = $HTTP_POST_VARS['poster'];

$message = $HTTP_POST_VARS['message'];

if(isset($HTTP_GET_VARS['parent']))

$parent = $HTTP_GET_VARS['parent'];

else

$parent = $HTTP_POST_VARS['parent'];

if(!$area)

$area = 1;

if(!$error) {

if(!$parent)

Trang 2

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

} } do_html_header($title);

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

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;

Listing 29.8 Continued

Trang 3

//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_quot-ing().These functions are all from the discussion_fns.phplibrary.They are shown in Listings 29.9, 29.10, and 29.11, respectively.

Listing 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();

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

}

Trang 4

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 >.

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.

Figure 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.

Trang 5

Listing 29.12 store_new_post.php—Puts the New Post in the Database

<?php include ('include_fns.php');

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

include ('index.php');

} else {

$error = true;

include ('new_post.php');

}

?>

As you can see, this is a short script Its main task is to call the store_new_post() func-tion.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;

} }

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

TỪ KHÓA LIÊN QUAN