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.
Next: Testing Our Content Feed >>
More JavaScript Articles
More By Mitchell Harper