Creating a Sample Content Feed
One of the reasons why content feeds are so popular is because 99% of the time, all you need to do to include someone else's content feed on your site is add a reference to an external JavaScript file using a HTML <script> tag, like this:
<script language="JavaScript" type="text/javascript"
src="http://www.jb-news.com/feed.php"></script>
Let's assume that I run a small Website and want to publish Joe Bloggs's content feed on my site using the code above. In it, I've set the src attribute to a file named feed.php on Joe's Website (and not a typical JavaScript file that ends with .js).
This isn't a problem, however, because both IE and Netscape don't care what the file extension is, provided that, when the file is grabbed, it actually contains valid JavaScript code. Let's create a sample content feed now. To start with, load the MySQL console application and create a new database named content:
create database content;
use content;
Next, create a new table named articles. This table will contain all our news items:
create table articles
(
articleId int auto_increment not null,
title varchar(100),
content text,
primary key(articleId)
);
The table we've just created should be pretty self-explanatory. It contains three fields:
articleId, an integer which is also a primary key and unique identifier, title, which will contain the headline of each news item, and content, a text field that will contain the body of each news item.Let's add three news items to our articles table using these MySQL commands:
insert into articles values(0, 'PHP scripting language takes
world by storm', 'It was announced today that at least two
million web sites around the world are using the PHP and
MySQL language/databasing technique to create
sites that are flexible');
insert into articles values(0, 'Linux includes G++ compiler',
'Linux, one of the world\'s most popular operating systems,
also includes a copy of both the GCC and G++ compilers, which
allow you to compile C/C++ program easily from the
command line');
insert into articles values(0, 'Microsoft announces C#',
'Microsoft have today announced that its new .NET framework
will be based around a language named C# (pronounced
C-Sharp). This language is much like C++ but is designed
for modern prorgammers');
|
Reader Rating- Reviews: 38 / Average score: 9.1 Next, we want to create the PHP script that will retrieve the news from our MySQL database and output it to the browser so that it appears as a bunch of JavaScript code. The PHP script's name is feed.php and it's available for download here. We'll concentrate only on the more important aspects of the script.
The first thing the script must do is declare that it will send out JavaScript code, as opposed to HTML. By default, PHP identifies the type of the output with a Content-Type: text/html header. To ensure that this script's output is identified as JavaScript, however, we must set the header ourselves.
To begin, our script creates four variables that will be used to log in to our MySQL server. I'm assuming that MySQL is installed on the same machine on which the
if(!$sConn || !$dConn) Next, we use these four variables to connect to our MySQL server and select the content database. We add the "@" symbol to the beginning of the If either the
Using a PHP script to output JavaScript may seem a little strange at first, but remember what I said earlier. All the user needs to do is set up a
Using the To actually output the news headlines as part of a HTML table, we use a PHP
There's nothing special about this The actual URL of each headline points to One important thing to note about the code inside the Sample output for one of the news items using the
That's actually all we need to do to create a content feed from our sample MySQL database. Let's now set up the content feed and test it in our browser. Testing Our Content Feed To test our content feed is simple. First, make sure that the
Run ![]() If you have a registered domain name (such as mysite.com) that points to a Web server running PHP and MySQL, then try to create the content database on that server. Upload the
|
|
Reader Rating- Reviews: 38 / Average score: 9.1 What makes our content feed dynamic? Many things. The fact that we have complete control over the feed.php script on our server means that we may change anything we like in it (e.g. to include a sponsor's ad). Any sites that already use our content feed won't have to do a thing to get the updated version, because it's retrieved from our server every time it's displayed on their site. Also, whenever we add a new item to our articles table, it will appear at the top of the content feed. Let's try adding one now. Using the MySQL console application, enter this query:
Return to your browser and hit the refresh button; notice the new entry in the list of news stories: ![]() It's just as easy to remove items from our content feed using the MySQL delete command:
This would remove those articles whose ![]() So, as you can see, using the Conclusion Take the sample content feed described throughout this article and modify it to match the look and feel of your site. You'll also need to modify the MySQL queries to extract the right fields from your database. Once you've customised the script for your needs, tell everyone about it: your ezine subscribers, friends, other Webmasters, etc. You'll be amazed by how many of them will agree to post it on their site because it's so simple to do. Remember, all that anyone needs to do to get your content feed is include HTML code similar to the following (with the URL of your feed.php file, of course) in any page of their site:
|
Click here to visit devArticles now! |