Ipower web hosting - WRITE YOUR OWN BLOG ENGINE // format search
Tuesday, January 22nd, 2008WRITE YOUR OWN BLOG ENGINE // format search for HTML display $q = stripslashes(htmlentities($q)); include(”functions.php”); ?>
Search Results
“; echo “
- \n”; do { $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; $summary = $myposts[”summary”]; echo “
- $title
- $summary
\n”; echo “
\n”; } while ($myposts = mysql_fetch_array($result)); echo “
“; } else { echo “
There were no posts matching your search for . $q.
“; } ?>
327
BLOG DESIGN SOLUTIONS What you have just done
Monday, January 21st, 2008BLOG DESIGN SOLUTIONS What you have just done is added a Fulltext index to the posts table on the title, summary, and post fields. Now when the database text is searched (I ll show you how in the next section), it will search all three fields, giving priority to the title, then the summary, then the post. So if you search for the word donkey , and one of your posts is titled Donkey Rides while another of your posts just has the word donkey somewhere in the post, Donkey Rides will be returned first in the search results because title is deemed to be more important than the post. And now to create the search results page, which will show the matching blog posts when someone uses the search form on any of your pages. The search results will look something like Figure 7-22. Figure 7-22. Search results page Here is the search results code in its entirety. Copy it into a new document and save it to your root directory as search.php.
WRITE YOUR OWN BLOG ENGINE Making (Web host forum) your blog
Monday, January 21st, 2008WRITE YOUR OWN BLOG ENGINE Making your blog searchable You no doubt have noticed the search form repeated throughout your blog. Thanks to MySQL, adding search capabilities to your blog is easy. All you need to do is add a search index to your posts table. To do this, open up phpMyAdmin in your browser, select the blog database, and click the posts link in the left frame. In the Indexes box (halfway down the screen), type 3 in the Create an index on n columns box and click Go (see Figure 7-20). Figure 7-20. Creating an index in PHPMyAdmin Now fill out the form as shown in Figure 7-21 and click Save. Figure 7-21. Adding a fulltext index to the posts table in phpMyAdmin 325
BLOG DESIGN SOLUTIONS If showbymonth is false, another (Frontpage web hosting)
Sunday, January 20th, 2008BLOG DESIGN SOLUTIONS If showbymonth is false, another do-while loop is started. The desired output for the loop is a heading for the year, followed by a list of months in that year:
2004
- November 2004 (1 post)
- December 2004 (4 posts)
2005
- January 2005 (9 posts)
- February 2005 (12 posts)
So the headings are not repeated with every row, the normal loop is preceded by some logic that determines whether there has been a change of year; if so, the list is closed, and a new heading is written. $plural = ($count==1) ? “” : “s”; echo “
\n”; } while ($myposts = mysql_fetch_array($result)); echo “
“; } break; } ?>
After the year headings have been checked for, the month is written with a link back to the archive page so that the posts for that month can be viewed on a page. Note the shorthand logic used to determine whether the word post should be singular or plural, depending on the number of posts for that month. The following line: $plural = ($count==1) ? “” : “s”; could also have been written as follows: if ($count==1) { $plural = “”; } else { $plural = “s”; }