697 Adding New Articles 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) 35 525x ch29 1/24/03 3:36 PM Page 697 698 Chapter 29 Building Web Forums { $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 ($parent is the postid of 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 35 525x ch29 1/24/03 3:36 PM Page 698 699 Adding New Articles //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.php library.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); } } 35 525x ch29 1/24/03 3:36 PM Page 699 700 Chapter 29 Building Web Forums 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); } The add_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.php script. 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.php page. Let’s look at the code for store_new_post.php. It is shown in Listing 29.12. 35 525x ch29 1/24/03 3:36 PM Page 700 701 Adding New Articles 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.php page, 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; } } 35 525x ch29 1/24/03 3:36 PM Page 701 . 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& apos;); $title = $HTTP_POST_VARS['title'];. get_post_title(), get_post_message(), and add_quot- ing().These functions are all from the discussion_fns .php library.They are shown in Listings 29.9, 29.10, and 29.11, respectively. Listing 29.9. $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