I'm not a very talented programmer, but I'm trying to put together a function that takes a few arrays as arguments and makes an RSS feed. I've checked some sample RSS feeds that I've got from this function and they all validate, but I'd like some input on what I could do to make this better. Here is the function with a sample implementation:
Which gives me the following output:
Thanks for your help.
PHP:
<?php
/* List of variables to pass:
$filename - filename of the file to create
$itemnum - number of items in the feed (not including channel info)
$titlearr - an array of titles, titlearr[0] is channel title
$descarr - an array of descriptions, descarr[0] is channel description
$linkarr - an array of links, linkarr[0] is channel link
*/
function makerss($filename, $itemnum, $titlearr, $descarr, $linkarr)
{
$rssfile = $filename;
$fh = fopen($rssfile, 'w');
fwrite($fh,"<rss version=\"2.0\">\n");
fwrite($fh,"<channel>\n\n");
fwrite($fh,"<title>".$titlearr[0]."</title>\n");
fwrite($fh,"<description>".$descarr[0]."</description>\n");
fwrite($fh,"<link>".$linkarr[0]."</link>\n\n");
for ($i=1;$i<$itemnum+1;$i++)
{
fwrite($fh,"<item>\n");
fwrite($fh,"<title>".$titlearr[$i]."</title>\n");
fwrite($fh,"<description>".$descarr[$i]."</description>\n");
fwrite($fh,"<link>".$linkarr[$i]."</link>\n");
fwrite($fh,"</item>\n\n");
}
fwrite($fh,"</channel>\n");
fwrite($fh,"</rss>");
fclose($fh);
return;
}
$titles = array( "the channel title",
"the first item title",
"second item title",
"third item title" );
$descs = array( "the channel description",
"the first item description",
"another item description",
"item number 3 description" );
$links = array ( "http://www.mainurl.com",
"http://www.mainurl.com/firstitem",
"http://www.mainurl.com/seconditem",
"http://www.mainurl.com/thirditem" );
makerss("sample.xml", 3, $titles, $descs, $links);
?>
Code:
<rss version="2.0">
<channel>
<title>the channel title</title>
<description>the channel description</description>
<link>http://www.mainurl.com</link>
<item>
<title>the first item title</title>
<description>the first item description</description>
<link>http://www.mainurl.com/firstitem</link>
</item>
<item>
<title>second item title</title>
<description>another item description</description>
<link>http://www.mainurl.com/seconditem</link>
</item>
<item>
<title>third item title</title>
<description>item number 3 description</description>
<link>http://www.mainurl.com/thirditem</link>
</item>
</channel>
</rss>
Thanks for your help.