So, you’ve spent the better part of a year creating your PHP/MySQL driven Website. You’ve got a reasonable amount of decent, flexible content, and you’re buddies with a couple of other Webmasters, so they’ve given you a free banner ad on their site in exchange for one on yours. What else can you do to promote your piÃ¨ce de rÃ©sistance?
A site’s promotion can be even more difficult than its development. I’ve found that one of the best ways to attract more visitors to your site is to share your content with whoever wants it. That’s right; let them publish your articles on their site (as long as they add a link back to your site near the end), write articles specifically for other Websites and ezines (hey, I’m doing that now aren’t I?), and set up a content feed from your site.
Just last week I set up a content feed similar to the one I’ll describe in this article. What’s that you say? "How do I set up a content feed? I only know PHP and MySQL!" Never fear — in this article I’ll show you how easy it is to set one up. But first, let’s discuss why you’d want to set up a content feed for your site.
Why A Content Feed?
I’ve said it before and I’ll say it again: if your site’s going to succeed on the Net, it needs to contain useful, free content targeted directly at a niche market.
Take a look at some of the most popular sites on the Web: ZDNet, Slashdot, Cnet, MoreOver, etc. All of these sites provide dynamic content feeds that anyone can use on their site, so that if I wanted to display their recent news headlines on my site, I would simply have to fill in a form and paste some HTML tags on my site, and I’d be done.
If Joe Bloggs posts the HTML code for ZDNet’s content feed on his site, then it’s a win-win situation: Joe provides valuable news to his visitors, which keeps them coming back on a daily basis, and ZDNet benefits from increased traffic when Joe’s visitors click on the news headlines to see the full story on their site.
A few reasons why you should consider creating your own content feed include:
- It’s one of the best forms of free advertising.
- Once other people post your content feed on their site, each headline actually links back to the full article your site. As many of you will know, the more links you have back to your site from other sites, the higher search engines such as Google will position you in their rankings.
- Obviously, having others post your content feed on their site will boost your traffic. By how much your traffic will increase depends on the quality of your content and how well you formulate your headlines.
Hopefully by now I’ve convinced you to set up a content feed. Let’s take a look at creating a sample content feed using some PHP and a MySQL database.
Creating a Sample Content Feed
<script> tag, like this:
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
create database 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,
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
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');
$db_server = "localhost";
$db_db = "content";
$db_user = "admin";
$db_pass = "password";
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
feed.php script will be set up.
$sConn = @mysql_connect($db_server, $db_user, $db_pass);
$dConn = @mysql_select_db($db_db, $sConn);
if(!$sConn || !$dConn)
document.write("Couldn't load news");
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
mysql_select_db commands fail, then
$dConn will contain no data. We use an if control to check for this, and if either one of them failed, we output
document.write("Couldn't load news"); to the browser.
$nResult = mysql_query("select articleId, title from articles order by
articleId desc limit 10");
mysql_queryfunction with a select statement returns the
titlefields for the ten articles most recently added to the articles table. The
limitkeyword makes sure that only ten are returned.
To actually output the news headlines as part of a HTML table, we use a PHP
document.writefunction to build the table's rows, like this:
while($nRow = mysql_fetch_array($nResult))
document.write(' <a target="_blank"
There's nothing special about this
whileloop. It uses PHP's
mysql_fetch_arrayfunction to retrieve the next record from the
$nResultresult set and outputs a table row that contains a hyper-linked news headline.
The actual URL of each headline points to
http://www.jb-news.com/news.phpand includes the ID of the article tacked onto the end. You can change that URL to a script on your PHP server and display the news based on the value of the newsId query string value. That's outside the scope of this article, however.
One important thing to note about the code inside the
whileloop is that it uses PHP's addslashes function to convert any single quotes in the title to escaped single quotes by adding a backslash to them. If we were to output an un-escaped quote as part of the argument to
Sample output for one of the news items using the
whileloop looks like this:
document.write(' <a target="_blank"
>Microsoft announces C#</a>');
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
feed.phpscript is in a directory within your Web server's document folder structure. Next, create a new HTML file named
test.htmland save it in the same directory as the
feed.phpscript (I'll assume that you've saved them both in the
/phpsubdirectory of your Web server's root directory). Enter the following HTML into
<title> Joe Bloggs News Test </title>
<h1>The Latest News</h1>
test.htmlin your Web browser. Assuming you used our sample
feed.php, which contains additional code to format the results, the page should look something like this:
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
feed.phpscript too. While keeping the test.html file on your local machine, replace its src attribute to point to the feed.php script on your Web server. Then run test.html in your browser -- it should appear exactly as before.
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:
insert into articles values(0, 'MySQL Version 4 Released', 'Today,
MysQL AB have released MySQL version 4. It comes packed with a number
of new features including transactions and faster
query execution plans');
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:
delete from articles where articleId < 3;
This would remove those articles whose
articeIdwas less than three, thus leaving two news headlines in our feed:
So, as you can see, using the
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: