JavaScript
  Home arrow JavaScript arrow Page 4 - Revisited: Creating an XML Content Feed Wi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Revisited: Creating an XML Content Feed With PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 7
    2002-10-28

    Table of Contents:
  • Revisited: Creating an XML Content Feed With PHP
  • Why a content feed?
  • Creating a sample content feed
  • Creating a sample content feed (contd.)
  • Testing Our Content Feed
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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("<tr>");

    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>");

    document.write("<tr>");

    <?php

    }


    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("<tr>");

    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>");

    document.write("<tr>");


    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.

    More JavaScript Articles
    More By Mitchell Harper


     

    JAVASCRIPT ARTICLES

    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables
    - Assigning Background Colors Dynamically to Z...
    - Building Zebra Tables with CSS and JavaScript
    - JavaScript: Array Objects
    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway