Home arrow JavaScript arrow Page 4 - Revisited: Creating an XML Content Feed With PHP

Revisited: Creating an XML Content Feed With PHP

This article was originally published early this year and is one of our most popular articles, republished by popular demand. Have a read and you will see why...

Author Info:
By: Mitchell Harper
Rating: 3 stars3 stars3 stars3 stars3 stars / 9
October 28, 2002
  1. · Revisited: Creating an XML Content Feed With PHP
  2. · Why a content feed?
  3. · Creating a sample content feed
  4. · Creating a sample content feed (contd.)
  5. · Testing Our Content Feed
  6. · Conclusion

print this article

Revisited: Creating an XML Content Feed With PHP - Creating a sample content feed (contd.)
(Page 4 of 6 )

Next, we use these four variables to connect to our MySQL server and select the content database. We prepend the mysql_connect and mysql_select_db function calls with the "@" symbol and save the return values to variables. This tells PHP not to write any connection errors to the browser. This is important in our script, because we only want it to return valid JavaScript code.

If either the mysql_connect or mysql_select_db commands fail, they $sConn or $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.

document.write is a JavaScript function. It takes one argument and outputs the contents of that argument directly to the browser.

Using a PHP script to output JavaScript may seem a little strange at first, but remember what I said earlier. All anyone that wants to use our content feed needs to do is setup a <script> tag with its src attribute referencing our feed.php file. As long as our script returns JavaScript code, then it will be processed in just the same way as inline JavaScript by the browser.

$nResult = mysql_query("select articleId, title from articles order by articleId desc limit 10");

Using the mysql_query function with a select statement returns the articleId and title fields for the ten most recent articles added to the articles table. The limit keyword takes care of making sure that only ten are returned.

To actually output the news headlines as part of a HTML table, we use a PHP while loop and output several calls to JavaScripts document.write function to build the tables rows, like this:

while($nRow = mysql_fetch_array($nResult))




document.write(" <td bgcolor='#FFEFCE' height='25'>");

document.write(" <p style='margin-left:5; margin-right:5'>");

document.write(" <a target='_blank' href='http://www.joe-bloggs-news.com/news.php?newsId=<?php echo $nRow["articleId"]; ?>'><font face='verdana' size='2' color='black'><?php echo str_replace("\"", "\\\"", $nRow["title"]); ?></font></a>");

document.write(" </td>");




There's nothing special about this while loop. It uses PHP’s mysql_fetch_array function to retrieve the next record from the $nResult resource and outputs a table row containing a hyper-linked news headline.

The actual URL of each headline is pointing to joe-bloggs-news.com/news.php and 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 while loop is that it uses PHP's str_replace function to convert any double quotes in the title to escaped double quotes. If we output an un-escaped double quote as part of the argument to document.write, then when that code is processed by JavaScript, it will assume that the double quote marks the end of the argument. This will cause a JavaScript syntax error.

Sample output for one of the news items using the while loop looks like this:


document.write(" <td bgcolor='#FFEFCE' height='25'>");

document.write(" <p style='margin-left:5; margin-right:5'>");

document.write(" <a target='_blank' href='http://www.joe-bloggs-news.com/news.php?newsId=3'><font face='verdana' size='2' color='black'>Microsoft announces C#</font></a>");

document.write(" </td>");


That's actually all we need to do to create a content feed from our sample MySQL database. Let’s now look at setting up the content feed and testing it in our browser.
blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials