FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


RSS Generator script





Jamatu
I've been working on a php script that gets the news from my mySQL database and outputs it to a xml file for rss feeds. What it basically does it open the test.xml file, then truncates it. It then re-opens the file and adds the header, body and footer. The header and footer are set strings but the body gets it's information from the database using a while loop to get the news articles and repeats everything inside <item></item> tags.

What do you guys think of it, what updates, fixes, additions etc should I make?

Code:
<?php
include 'config.php';
include 'dbconnect.php';
$filename = 'test.xml';
$openerr = "Cannot open $filename";
$writeerr = "Cannot write to $filename";
$header = "<rss version=\"2.0\">
<channel>

<title>SL-1</title>
<description>Only a website template at the minute, still need to decide what to base website on!</description>
<link>http://192.168.2.10/</link>
<language>en-uk</language>
<ttl>60</ttl>

";
$footer = "</channel>
</rss>";

//Code to clear the file before we add the new xml code
if (is_writable($filename)) {
   $handle = fopen($filename, 'w') or die("$openerr");
   if (fwrite($handle,'',0) === FALSE) {
      echo "$writeerr";
      exit;
   }
   fclose($handle);
} else {
   echo "$filename is unwritable, chmod should be set to 666";
   exit;
}

//Open the file with mode a (add string to the end of a file so we are able to add the header, body and footer strings without overwriting each other.
$handle = fopen($filename, 'a') or die("$openerr");
if (fwrite($handle, $header) === FALSE) {
   echo "$writeerr";
   exit;
}
$query = "SELECT * FROM news ORDER BY news_id DESC LIMIT 0,5";
$result=mysql_query($query) or die (mysql_error());
mysql_close();
while($row = mysql_fetch_row($result))
{
   $news_id    = $row[0]; //use row names for simplicity
   $news_time   = $row[1];
   $news_title  = $row[2];
   $news_text   = htmlentities(nl2br($row[3])); //Convert html entities so xml doesn't parse them but rather the aggregators
   $news_time   = date('D, M j Y H:i:s O', $news_time); //convert UNIX time to an aggregator friendly format
   
   $body="<item>
   <title>$news_title</title>
   <description>$news_text</description>
   <pubDate>$news_time</pubDate>
   <guid isPermaLink=\"true\">http://192.168.2.10/newsarchive.php?id=$news_id</guid>
   </item>
      
   ";
   if (fwrite($handle, $body) === FALSE) {
   echo "$writeerr";
   exit;
   }
}

if (fwrite($handle, $footer) === FALSE) {
   echo "$writeerr";
   exit;
}
echo "RSS feed updated succesfully <br />File: $filename";
fclose($handle);
?>
Related topics
Form Generator
RSS
Script de NEWS
[php] can't display rss feeds using rss2html.php
Pulling quotes from wikiquote
RSS integration?
Javascript random code generator script needed please
I have some scripts but don't know what to do with them?
Phpbb and PhpNuke Help
comment integrer le script googleadsense dans une page php
Excellent website for Javascripts
The most optimal PHP prime spiral generator yet
RSS feeds from my Frihost site
Numero Uno
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.