Sunday, 18 August 2013

Dynamically generating a RSS feed

Dynamically generating a RSS feed

I might be thinking about this too deeply, but I'd still like to know if
anybody has any strong opinions on it...
I'm generating a news RSS feed using PHP. Each news story has many photos,
which is reflected in my database structure with a 1:many relationship
between the Stories and Photos tables. This means I'm generating a results
set which looks something like:
StoryIDPhotoID
1 1
1 2
1 3
2 4
2 5
3 6
4 7
4 8

I'm generating a nested array to represent this more accurately:
I'm generating this just using a foreach statement and if to see if the
StoryID already exists when dealing with each new row - is there a better
way? Is there a built-in function / 3rd party library which can deal with
1:many relationships efficiently?)
There are then 3 possible options - are any of these philosophically better?:
Modify the code so that the XML is generated without generating the array 1st
Leave it as an array (as this seemed logical) and then use SimpleXML to
rewrite it as XML (this also means that if I want to return the info as
JSON, or any other format, I don't have rewrite the logic again and can
just format the array) - however, is this particularly slow? Especially
for a large number of RSS items?
whichever of the above, I know some people who use PHP to write a static
RSS file when the PHP is called (a new file is only generated if there is
new data to be added), rather than generate the RSS file on the fly each
time - are there any advantages to this method?

No comments:

Post a Comment