使用简单饼图分析多个提要而不合并它们


Parsing Multiple feeds with Simple Pie without merging them

我使用Simplepie从DB表中获取rss提要。我试图在它们各自的div(例如一个或多个不同的列)上显示这些提要,它们各自的项目数量也来自数据库。我有效地将每个提要视为SimplePie的一个单独实例。

我的问题是,我可以实例化与数据库中rss提要一样多的Simplepie对象实例吗(请参阅下面的概念验证代码)。这对我手动使用两个提要是有效的,但我想知道一个用户是否有50个提要,是否可以继续这样实例化SimplePie对象,或者有更好的方法吗

我担心可扩展性和正确性。

这就是我目前正在做的事情:

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 
...

我的另一个问题是,我如何以编程方式创建更多的变量$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}`或者有更好的方法吗?

谢谢!

您的方法基本上是好的IMO,尽管如果每个用户实例化50个对象,您可能会看到一些缓慢的页面加载时间。

我不会实例化单独的变量,而是使用一个数字索引数组,只需按照您希望它们在中的顺序从数据库返回提要url

$feeds = array();
// get feeds from db
$feeds[1] = $url_1; // first url from db query
...
$feed->set_feed_url($feeds[1]);
// etc.