Reputation: 1648
I'm using Simplepie to fetch rss feeds from a DB table. I'm trying to display these feeds on their own respective divs (e.g. one or more different columns) with their own respective number of items also coming from a DB. I am effectively treating each feed as a separate instance of SimplePie.
My question is, is it ok for me to instantiate as many instances of Simplepie objects as there are rss feeds in the db (see proof of concept code below). This works for me with two feeds manually but I am wondering if a user has 50 feeds is it ok to keep instantiating SimplePie objects like this or is there a better way?
I am worried about scalability and correctness.
This is what I am currently doing:
require_once('../php/autoloader.php');
// We'll process this feed with all of the default options.
$feed = new SimplePie();
$feed2 = new SimplePie();
// Set which feed to process.
$feed->set_feed_url('http://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml');
$feed2->set_feed_url('http://www.synthtopia.com/feed/');
// Run SimplePie.
$feed->init();
$feed2->init();
// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set
$feed->handle_content_type();
$feed2->handle_content_type();
// Then display feeds
...
My other qeustion is, how can I programmatically create more variables $feed3, $feed4, $feed5' etc without having to write them manually. Do I just do a forerch and append {$i} to feeds like so?
${"feed" . $i}` or is there a better way of doing this?
Thanks!
Upvotes: 0
Views: 601
Reputation: 2109
Your approach is basically fine IMO although if every user instantiates 50 objects, you could see some slow page load times.
Instead of instantiating separate variables, I would use a numeric indexed array and just return the feed url from the database in the order you want them to be in
$feeds = array();
// get feeds from db
$feeds[1] = $url_1; // first url from db query
...
$feed->set_feed_url($feeds[1]);
// etc.
Upvotes: 1