Hieronder de code en het spreekt denk ik voor zich. Mijn probleem is dat als ik een item wil toevoegen hij hem wel aan de $this->items binnen de methode toevoegd, maar niet aan de $this->items van de feed class. Waarschijnlijk is dit een redelijk vaag verhaal, maar ik ben nog niet helemaal thuis in de terminologie van OOP.
<?php
class Feed
{
var $title;
var $link;
var $description;
var $language = "nl-NL";
var $pubDate;
var $self;
var $image;
var $items;
var $feed;
function Feed($title, $link, $description, $self)
{
$this->title = $title;
$this->link = $link;
$this->description = $description;
$this->self = $self;
}
function addImage($url)
{
$this->image = "<image>\n
<url>" . $url . "</url>\n
<title>" . $this->title . "</title>\n
<link>". $this->link . "</link>\n
</image>";
return $this->image;
}
function addItem($item)
{
$this->items .= $item;
}
function serve()
{
$this->feed = "<?xml version='1.0' encoding='ISO-8859-1'?>\n
<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:atom='http://www.w3.org/2005/Atom'>\n
<channel>\n
<title>" . $this->title . "</title>\n
<link>" . $this->link . "</link>\n
<description>" . $this->description . "</description>\n
<language>" . $this->language . "</language>\n
<atom:link href='".$this->self."' rel='self' type='application/rss+xml' />\n
" . $this->image . $this->items . "
</channel>\n
</rss>";
header("Content-type: application/xml");
echo $this->feed;
}
}
class Item
{
var $title;
var $link;
var $description;
var $pubDate;
var $guid;
var $item;
function Item($title, $link, $description, $pubDate, $guid)
{
$this->title = $title;
$this->link = $link;
$this->description = $description;
$this->pubDate = $this->setPubDate($pubDate);
$this->guid = $guid;
if(empty($this->guid)) $this->guid = $this->link;
}
function setPubDate($date)
{
if(strtotime($date) == false)
$this->pubDate = date("D, d M Y H:i:s ", $date) . "+0100";
else
$this->pubDate = date("D, d M Y H:i:s ", strtotime($date)) . "+0100";
}
function addItem($feed)
{
$this->item = "<item>\n<title>" . $this->title . "</title>\n<link>" . $this->link . "</link>\n<description>" . $this->description . "</description>\n<pubDate>" . $this->pubDate . "</pubDate>\n<guid>" . $this->guid . "</guid>\n</item>\n";
$feed->addItem($this->item);
}
}
?>
<?php
// SQL Query, die is goed.
$feed = new Feed('v.v. Gendringen nieuws', 'http://www.vvgendringen.nl/index.php', 'Nieuwsfeed met al het nieuws binnen voetbalvereniging Gendringen.', 'http://www.vvgendringen.nl/rss.php');
while($result = mysql_fetch_assoc($res))
{
$item = new Item($result['title'], 'http://www.vvgendringen.nl/nieuws/'.$result['id'].'/'. preg_replace("/[^a-z0-9]+/i", "-", strtolower($result['titel'])) . '.html', strip_tags(html_entity_decode($result['inleiding']), '<img><br><em><strong><span><a>'), $result['date'], null);
$item->addItem($feed);
}
$feed->serve();
?>
Hopelijk kunnen jullie mij van dienst zijn.