PHP for Absolute Beginners PHẦN 8 pdf

41 272 0
PHP for Absolute Beginners PHẦN 8 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CHAPTER 9  SYNDICATING THE BLOG 268 // Remove the fulldisp flag array_pop($e); // Perform basic data sanitization $e = sanitizeData($e); // Add a content type header to ensure proper execution header('Content-Type: application/rss+xml'); // Output the XML declaration echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <rss version="2.0"> <channel> <title>My Simple Blog</title> <link>http://localhost/simple_blog/</link> <description>This blog is awesome.</description> <language>en-us</language> </channel> </rss> At this point, you have an array that contains your entries ready for output. You use a foreach loop to display each entry. Begin by generating an item with a title, a description, and a link. The only extra step is to escape all HTML entities in your description—you need to convert special characters to their HTML entity equivalents, such as < to &lt;. This is necessary because unescaped HTML will cause an error for RSS readers. You accomplish this by calling htmlentities() on the contents of the entry column. Add the following code in bold to rss.php: <?php // Include necessary files include_once ' /inc/functions.inc.php'; include_once ' /inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Load all blog entries $e = retrieveEntries($db, 'blog'); Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 269 // Remove the fulldisp flag array_pop($e); // Perform basic data sanitization $e = sanitizeData($e); // Add a content type header to ensure proper execution header('Content-Type: application/rss+xml'); // Output the XML declaration echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <rss version="2.0"> <channel> <title>My Simple Blog</title> <link>http://localhost/simple_blog/</link> <description>This blog is awesome.</description> <language>en-us</language> <?php // Loop through the entries and generate RSS items foreach($e as $e): // Escape HTML to avoid errors $entry = htmlentities($e['entry']); // Build the full URL to the entry $url = 'http://localhost/simple_blog/blog/' . $e['url']; ?> <item> <title><?php echo $e['title']; ?></title> <description><?php echo $entry; ?></description> <link><?php echo $url; ?></link> </item> <?php endforeach; ?> </channel> </rss> Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 270 To keep your script as legible as possible, you need to use the alternative syntax for your foreach loop, using a colon to start the loop and endforeach to close the loop. You can see your feed items displayed if you load http://localhost/simple_blog/ feeds/rss.php into your browser at this point (see Figure 9-2). Figure 9-2. Your feed with items, as displayed in Firefox 3 On the most basic level, your RSS feed is now running. However, you still need to add a couple things to feed items to ensure widespread compatibility with feed readers: a GUID and a publishing date. Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 271 What Is a GUID? In RSS, publishers are encouraged to include a Globally Unique Identifier (GUID) . There is no hard-and- fast rule for what to use as an item’s GUID, but the most common GUID tends to be the permanent URL of the item because this is generally a unique path that won’t be duplicated. In your feed, you use each item’s URL as a GUID, which means that adding it is as simple as including a second element that displays the link value. It’s not uncommon for RSS feed items to have identical link and GUID values. You can insert a GUID by adding the code in bold to rss.php: <?php // Include necessary files include_once ' /inc/functions.inc.php'; include_once ' /inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Load all blog entries $e = retrieveEntries($db, 'blog'); // Remove the fulldisp flag array_pop($e); // Perform basic data sanitization $e = sanitizeData($e); // Add a content type header to ensure proper execution header('Content-Type: application/rss+xml'); // Output the XML declaration echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <rss version="2.0"> <channel> <title>My Simple Blog</title> <link>http://localhost/simple_blog/</link> <description>This blog is awesome.</description> <language>en-us</language> Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 272 <?php // Loop through the entries and generate RSS items foreach($e as $e): // Escape HTML to avoid errors $entry = htmlentities($e['entry']); // Build the full URL to the entry $url = 'http://localhost/simple_blog/blog/' . $e['url']; ?> <item> <title><?php echo $e['title']; ?></title> <description><?php echo $entry; ?></description> <link><?php echo $url; ?></link> <guid><?php echo $url; ?></guid> </item> <?php endforeach; ?> </channel> </rss> What Is a Publishing Date? The publishing date of a feed item is the date it was created on. RSS requires that this date conform to the RFC-822 guidelines, which means the date must be formatted as follows: Sat, 23 May 2009 18:54:16 -0600 You might remember that, when you created your entries table, you included a column called created that stores the date automatically when an entry is created. However, as things stand now, the created column isn’t returned from retrieveEntries(). Before you can generate a publishing date for your feed items, you need to add the created column to the array of values returned from retrieveEntries(). Modifying retrieveEntries() to Return the created Column Your first task is to add the created column to the array returned from retrieveEntries(). To do this, you simply need to add the created column to your SQL query. Do this by opening functions.inc.php and modifying the lines in bold in retrieveEntries(): Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 273 function retrieveEntries($db, $page, $url=NULL) { /* * If an entry URL was supplied, load the associated entry */ if(isset($url)) { $sql = "SELECT id, page, title, image, entry, created FROM entries WHERE url=? LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute(array($url)); // Save the returned entry array $e = $stmt->fetch(); // Set the fulldisp flag for a single entry $fulldisp = 1; } /* * If no entry ID was supplied, load all entry titles for the page */ else { $sql = "SELECT id, page, title, image, entry, url, created FROM entries WHERE page=? ORDER BY created DESC"; $stmt = $db->prepare($sql); $stmt->execute(array($page)); $e = NULL; // Declare the variable to avoid errors // Loop through returned results and store as an array while($row = $stmt->fetch()) { if($page=='blog') { $e[] = $row; $fulldisp = 0; } Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 274 else { $e = $row; $fulldisp = 1; } } /* * If no entries were returned, display a default * message and set the fulldisp flag to display a * single entry */ if(!is_array($e)) { $fulldisp = 1; $e = array( 'title' => 'No Entries Yet', 'entry' => 'This page does not have an entry yet!' ); } } // Add the $fulldisp flag to the end of the array array_push($e, $fulldisp); return $e; } Creating a pubDate from the MySQL Timestamp Now your entry array contains the MySQL timestamp that was generated when each entry was created. However, the MySQL timestamp is formatted like this: 2009-05-23 18:54:16 Using the MySQL timestamp renders your RSS feed invalid, so you need to somehow convert it to the proper RFC-822 format. Fortunately, PHP provides two functions that allow you to do exactly that: strtotime() and date(). This means that string to timestamp—strtotime()—can convert a variety of date strings, including your MySQL timestamp, to a UNIX timestamp. You can then use the UNIX timestamp in the date() function, which accepts a format and a timestamp and outputs a formatted date string. RSS is widely used, so PHP provides a constant for use with date() that returns a properly formatted RFC-822 date string, called DATE_RSS. Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 275 The next step is to add the code in bold to rss.php to reformat your date and add a publishing date to each item: <?php // Include necessary files include_once ' /inc/functions.inc.php'; include_once ' /inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Load all blog entries $e = retrieveEntries($db, 'blog'); // Remove the fulldisp flag array_pop($e); // Perform basic data sanitization $e = sanitizeData($e); // Add a content type header to ensure proper execution header('Content-Type: application/rss+xml'); // Output the XML declaration echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; ?> <rss version="2.0"> <channel> <title>My Simple Blog</title> <link>http://localhost/simple_blog/</link> <description>This blog is awesome.</description> <language>en-us</language> Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 276 <?php // Loop through the entries and generate RSS items foreach($e as $e): // Escape HTML to avoid errors $entry = htmlentities($e['entry']); // Build the full URL to the entry $url = 'http://localhost/simple_blog/blog/' . $e['url']; // Format the date correctly for RSS pubDate $date = date(DATE_RSS, strtotime($e['created'])); ?> <item> <title><?php echo $e['title']; ?></title> <description><?php echo $entry; ?></description> <link><?php echo $url; ?></link> <guid><?php echo $url; ?></guid> <pubDate><?php echo $date; ?></pubDate> </item> <?php endforeach; ?> </channel> </rss> Your feed now contains all the elements it needs to be compatible with nearly all feed readers. When you load http://localhost/simple_blog/feeds/rss.php in a browser, you see your items displayed with the date they were published (see Figure 9-3). Download at WoweBook.Com CHAPTER 9  SYNDICATING THE BLOG 277 Figure 9-3. Your feed as it appears in Firefox 3, complete with the publishing date Download at WoweBook.Com [...]... $url); // Format the image if one exists $img = formatImage($e['image'], $e['title']); if($page=='blog') { // Load the comment object include_once 'inc/comments.inc .php' ; $comments = new Comments(); $comment_form = $comments->showCommentForm($e['id']); } else { $comment_form = NULL; } ?> < ?php echo $e['title'] ?> < ?php echo $img, $e['entry'] ?> < ?php echo $admin['edit'] ?> < ?php if($page=='blog')... of the available formatting characters For a full reference of such characters, look up the date() entry in the PHP manual at http:/ /php. net/date You store your formatting string in a variable called $format, which makes it easy to identify and change in the future, if necessary Use this formatting string for your comment byline: F j, Y \a\\t g:iA Note In the “ F j, Y \a\\t g:iA” formatting string,... include_once 'inc/comments.inc .php' ; $comments = new Comments(); $comment_disp = $comments->showComments($e['id']); $comment_form = $comments->showCommentForm($e['id']); } else { $comment_form = NULL; } ?> < ?php echo $e['title'] ?> < ?php echo $img, $e['entry'] ?> < ?php echo $admin['edit'] ?> < ?php if($page=='blog') echo $admin['delete'] ?> < ?php if($page=='blog'): ?> ... Modifying update.inc .php to Handle New Comments Your script knows how to handle data from the comment form, so you’re ready to modify update.inc .php to call saveComment() when the comment form is submitted You can do this by adding a check to see whether the user clicked the Post Comment button You add this check after your check for the admin form in update.inc .php If the comment form was submitted,... similar approach here, changing the format of the MySQL timestamp stored in the database to fit your needs You use the strtotime() and date() methods again, but this time you need to create a custom date formatting string FORMATTING DATES IN PHP USING THE DATE() FUNCTION PHP provides you with many options for formatting dates when you use the date() function You can format each part of a date (day, month,... comment entries • Build a Comments class to perform all comment-related actions • Build a method for displaying a form to enter new comments • Modify index .php to display the comment form • Build a method to store comments in the comments table • Modify update.inc .php to handle new comments • Build a method to retrieve comments for an entry • Modify index .php to display entry comments • Build a method... $admin['delete'] ?> < ?php if($page=='blog'): ?> Back to Latest Entries < ?php echo $comment_form; endif; ?> < ?php } // End the if statement 288 Download at WoweBook.Com CHAPTER 10 ADDING A COMMENTING SYSTEM TO YOUR BLOG Viewing a full entry in a browser reveals your new comment form beneath the entry (see Figure 10-2) Figure 10-2 Your comment form Storing New Comments... place to see the form, you need to include your comment class, instantiate it, and load the form into a variable, which you can call $comment_form Finally, you add the comment form to the output, just after the Back to Latest Entries link Download at WoweBook.Com 287 CHAPTER 10 ADDING A COMMENTING SYSTEM TO YOUR BLOG To do this, add the code in bold to index .php, starting at line 78: // If the full... construct() { // Open a database connection and store it $this->db = new PDO(DB_INFO, DB_USER, DB_PASS); } // Display a form for users to enter new comments with public function showCommentForm($blog_id) { return . BLOG 287 // Display a form for users to enter new comments with public function showCommentForm($blog_id) { return <<<FORM <form action="/simple_blog/inc/update.inc .php& quot;. which accepts a format and a timestamp and outputs a formatted date string. RSS is widely used, so PHP provides a constant for use with date() that returns a properly formatted RFC -82 2 date string,. timestamp is formatted like this: 2009-05-23 18: 54:16 Using the MySQL timestamp renders your RSS feed invalid, so you need to somehow convert it to the proper RFC -82 2 format. Fortunately, PHP provides

Ngày đăng: 12/08/2014, 16:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan