Archive for the 'Apache' Category

Ipower web hosting - WRITE YOUR OWN BLOG ENGINE // format search

Tuesday, January 22nd, 2008

WRITE YOUR OWN BLOG ENGINE // format search for HTML display $q = stripslashes(htmlentities($q)); include(”functions.php”); ?>

Search Results

There $plural1 $numresults post$plural2 . matching your search for $q.

“; echo “

\n”; do { $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; $summary = $myposts[”summary”]; echo “
$title

\n”; echo “

$summary

\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, 2008

BLOG 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, 2008

WRITE 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, 2008

BLOG 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

2005

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 “

  • . $monthyear ($count post$plural)
  • \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”; }

    WRITE YOUR OWN BLOG ENGINE The results are (Shared web hosting)

    Saturday, January 19th, 2008

    WRITE YOUR OWN BLOG ENGINE The results are finally ordered by year and then month. Skipping through the code to the posts div:

    Archive

    The textual value of the month and year is inserted in the heading. \n”; do { $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; $summary = $myposts[”summary”]; echo “

    . $title

    \n”; echo “

    $summary

    \n”; } while ($myposts = mysql_fetch_array($result)); echo “

    “; } break; If showbymonth is true, the familiar do-while loop is executed to display a list of posts for the selected month. case false: $previousyear = “”; if($myposts) { do { $year = $myposts[”year”]; $month = $myposts[”month”]; $monthyear = $myposts[”monthyear”]; $count = $myposts[”count”]; if ($year != $previousyear) { if ($previousyear != “”) { echo “

    \n”; } echo “

    $year

    “; echo “

      \n”; $previousyear = $year; } 323

    BLOG DESIGN SOLUTIONS If the year and month

    Friday, January 18th, 2008

    BLOG DESIGN SOLUTIONS If the year and month look correct, the posts are retrieved. These posts are identified by using the MONTH and YEAR MySQL functions to match the postdate to the year and month specified in the query string: if ($myposts) { $showbymonth = true; $text = strtotime(”$month/1/$year”); $thismonth = date(”F Y”, $text); } } If the database query returns a result (that is, it finds one or more posts for that month), the $showbymonth flag is set to true so the page knows it is showing a month archive. Then the strtotime and date functions are combined to convert the numeric month and year into more readable text such as April 2005. The strtotime function takes a string that looks like a date and tries to turn it into a timestamp that can be processed by PHP. The date function takes a timestamp and formats it into text (much like the MySQL date_ format function mentioned earlier in the chapter). if (!isset($showbymonth)) { $showbymonth = false; // Select posts grouped by month and year $sql = “SELECT DATE_FORMAT(postdate, ‘%M %Y’) AS monthyear, . MONTH(postdate) AS month, YEAR(postdate) AS year, count(*) AS count . FROM posts GROUP BY monthyear . ORDER BY year, month”; $result = mysql_query($sql); $myposts = mysql_fetch_array($result); } If the $showbymonth flag has not been set (because no posts were found for the specified month, or if no valid month or year were specified), the flag is set to false and the database is queried for all posts. There are a number of new concepts introduced in this SQL query, so I will step through it bit by bit: SELECT DATE_FORMAT(postdate, ‘%M %Y’) AS monthyear, The postdate for each post is formatted as month and year; for example, April 2005. MONTH(postdate) AS month, YEAR(postdate) AS year, The postdate is also formatted as a month and a year separately. These values will be used to construct a link to the archive. count(*) AS count FROM posts GROUP BY monthyear GROUP BY monthyear is used to combine all the posts that have the same value of monthyear; for example, all the posts made in April 2005. The count(*) function can then be used to output the number of posts that have been grouped together for that value of monthyear; in other words, the number of posts made in April 2005. ORDER BY year, month

    WRITE YOUR OWN BLOG ENGINE echo ” . (Frontpage web hosting)

    Friday, January 18th, 2008

    WRITE YOUR OWN BLOG ENGINE echo “

  • . $monthyear ($count post$plural)
  • \n”; } while ($myposts = mysql_fetch_array($result)); echo “

    “; } break; } ?>

    Stepping through the code:

    Web server - BLOG DESIGN SOLUTIONS Archive

    Thursday, January 17th, 2008

    BLOG DESIGN SOLUTIONS

    Archive

    \n”; do { $post_id = $myposts[”post_id”]; $title = $myposts[”title”]; $summary = $myposts[”summary”]; echo “

    . $title

    \n”; echo “

    $summary

    \n”; } while ($myposts = mysql_fetch_array($result)); echo “

    “; } break; case false: $previousyear = “”; if($myposts) { do { $year = $myposts[”year”]; $month = $myposts[”month”]; $monthyear = $myposts[”monthyear”]; $count = $myposts[”count”]; if ($year != $previousyear) { if ($previousyear != “”) { echo “

    \n”; } echo “

    $year

    “; echo “

      \n”; $previousyear = $year; } $plural = ($count==1) ? “” : “s”;

    Photoshop web design - WRITE YOUR OWN BLOG ENGINE Here is the

    Wednesday, January 16th, 2008

    WRITE YOUR OWN BLOG ENGINE Here is the entire code for the archive page. Save it as archive.php in your root directory: