Need Comments on a Function for Making RSS Feeds with PHP

potentialeight

Expert Gambling Writer
Oct 30, 2010
2,201
41
0
NC
www.potentialeight.com
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:

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

?>
Which gives me the following output:

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.
 


Hi potentialeight, Your valuable programming will help me a lot. Thanks for the post.

lol spam.

My only comment is that lots of seperate writes isn't very efficient, but you shoudonly worry about that if you're making millions of feeds. Good job!
 
lol spam.

My only comment is that lots of seperate writes isn't very efficient, but you shoudonly worry about that if you're making millions of feeds. Good job!
So something like this would be better because of the fewer writes?

Code:
 <?php

function makerss($filename, $itemnum, $titlearr, $descarr, $linkarr)
{
  $rssfile = $filename;
  $fh = fopen($rssfile, 'w');

  fwrite($fh,"<rss version=\"2.0\">\n<channel>\n\n<title>".$titlearr[0]."</title>\n<description>".$descarr[0]."</description>\n<link>".$linkarr[0]."</link>\n\n");

  for ($i=1;$i<$itemnum+1;$i++)
  {
    fwrite($fh,"<item>\n<title>".$titlearr[$i]."</title>\n<description>".$descarr[$i]."</description>\n<link>".$linkarr[$i]."</link>\n</item>\n\n");
  }

  fwrite($fh,"</channel>\n</rss>");

  fclose($fh);
  return;
}

?>
P.S. I put it into code tags instead of PHP tags since the PHP colors are hard to read on this background.
 
That's much better code. Why not throw alot of links in, and test the theory yourself. Time them both.

On another note, rather than passing in the number of items, why not just use the count() function on the array, and determine the number of links from that?
 
an alternative, to keep it readable, is to have an $output string, which you append to, then write out once at the end.
 
That's much better code. Why not throw alot of links in, and test the theory yourself. Time them both.

On another note, rather than passing in the number of items, why not just use the count() function on the array, and determine the number of links from that?
I didn't mention it in the OP, but I'm going to often be dealing with situations where I don't want RSS feeds made from the entire arrays. I agree that would be a better way to do it, including checks to make sure the arrays were the same size, etc., otherwise.

an alternative, to keep it readable, is to have an $output string, which you append to, then write out once at the end.
Yeah I don't know why this didn't occur to me since I've done similar before.

Thanks both.