Logo         Community
The Company
Your Account
Contact Us
PHP 101 (part 15): No News is Good News
Build your own personal RSS aggregator with PHP, SimpleXML and SQLite

| Top Story |

With the database safely in its web-inaccessible directory, the next step is to write the code that uses the data inside it to connect to each news feed, parse it for news data, and present a customized news page.

Here's what that code, user.php, looks like:

// PHP 5

// include configuration file

// open database file
$handle = sqlite_open($db) or die('ERROR: Unable to open database!');

// generate and execute query
$query = "SELECT id, title, url, count FROM rss";
$result = sqlite_query($handle, $query) or die("ERROR: $query. ".sqlite_error_string(sqlite_last_error($handle)));

// if records present
if (sqlite_num_rows($result) > 0) {

    // iterate through resultset
    // fetch and parse feed
    while($row = sqlite_fetch_object($result)) {

        $xml = simplexml_load_file($row->url);
        echo "<h4>$row->title</h4>";
        // print descriptions
        for ($x = 0; $x < $row->count; $x++) {

            // for RSS 0.91
            if (isset($xml->channel->item)) {
                $item = $xml->channel->item[$x];
            // for RSS 1.0
            elseif (isset($xml->item)) {
                $item = $xml->item[$x];
            echo "<a href=\"$item->link\">$item->title</a><br />$item->description<p />";
        echo "<hr />";

        // reset variables
// if no records present
// display message
else {

    <font size = '-1'>No feeds currently configured</font>


// close connection

Here's what the output might look like (note that there will a time lag in producing the page, because PHP will be silently opening HTTP connections to each URL to retrieve the corresponding RSS feed):

Output image

The code to accomplish this might look simple, but there's actually a lot going on behind the scenes. The first step is to obtain a list of the RSS feeds configured by the user from the SQLite database. To accomplish this, a SQLite database handle is initialized, and a SQL SELECT query is executed. A while() loop is used to iterate through the resulting record collection.

For each URL thus obtained, the simplexml_load_file() function is used to retrieve and read the RSS feed. Depending on the number of stories to be displayed, a for() loop is executed and the appropriate number of <item> elements in the feed are parsed. Notice that the path to access an <item> differs depending on whether the feed is RSS 0.91 or RSS 1.0.

Note that if the database is empty, an error message will appear. In this example, since I've already inserted a bunch of records into the database, you'll never see the error message at all; however, it's good programming practice to ensure that all eventualities are accounted for, even remote ones.

As before, the file config.php is included at the top of every script. This file contains database access parameters, as below:

// database details
// always use a directory that cannot be accessed from the web
$path = $_SERVER['DOCUMENT_ROOT'].'/../';
$db = $path.'rss.db';

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback