Return to Snippet

Revision: 11239
at February 10, 2009 11:28 by antpaw


Updated Code
<?php
error_reporting(E_ALL);

mysql_connect("localhost","root","root") or die (mysql_error());
mysql_select_db("oop") or die (mysql_error());

class RSS {

var $XMLdump;

var $pagetitle;
var $pagelink;
var $pegedescription;
var $pagelanguage;

var $sqlresult;

function setHead($setPagetitle, $setPagelink, $setPegedescription, $setPagelanguage){
	$this->pagetitle = $setPagetitle;
	$this->pagelink = $setPagelink;
	$this->pegedescription = $setPegedescription;
	$this->pagelanguage = $setPagelanguage;
}

function getDataFrom($setSQL){
	$this->sqlresult = mysql_query($setSQL);
}


function rssHead(){
	$this->XMLdump = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom/\">
	<channel>
		<title>".$this->pagetitle."</title>
		<link>".$this->pagelink."</link>
		<description>".$this->pegedescription."</description>
		<language>".$this->pagelanguage."</language>
		<lastBuildDate>".date("r", time())."</lastBuildDate>\n";
}

function rssItems(){
	while($bla = mysql_fetch_assoc($this->sqlresult)){
		$this->XMLdump .= "		<item>\n";
		$this->XMLdump .= "			<title>".$bla['title']."</title>\n";
		$this->XMLdump .= "			<link>http://bestnewssiteever.com/news/".$bla['id']."/</link>\n";
		$this->XMLdump .= "			<category>".$bla['category']."</category>\n";
		$this->XMLdump .= "			<pubDate>".date("r",$bla['pubDate'])."</pubDate>\n";
		preg_match_all("/^(?:[^.]*\.){3}/", $bla['content'], $trimedContent);
		$this->XMLdump .= "			<description>".$trimedContent[0][0]."..</description>\n";
		$this->XMLdump .= "		</item>\n";
	}
}

function rssFooter(){
	$this->XMLdump .= " 	</channel>
</rss>";
}

function writeXML(){
	$this->rssHead();
	$this->rssItems();
	$this->rssFooter();
	return $this->XMLdump;
}

function saveXML($file){
	$fp = fopen($file,"w+");
	flock($fp,2);
	fwrite($fp,$this->writeXML());
	flock($fp,3);
	fclose($fp);
}
}

$Bar = new RSS();
$Bar->getDataFrom("SELECT * FROM news ORDER BY pubDate DESC");
$Bar->setHead("TITLE","http://domain.de","DESCRIPTION","en-EN");
$Bar->saveXML("blub.xml");

?>

Revision: 11238
at January 28, 2009 15:50 by antpaw


Initial Code
<?php
error_reporting(E_ALL);

mysql_connect("localhost","root","root") or die (mysql_error());
mysql_select_db("oop") or die (mysql_error());

class RSS {

var $XMLdumb;

var $pagetitle;
var $pagelink;
var $pegedescription;
var $pagelanguage;

var $sqlresult;

function setHead($setPagetitle, $setPagelink, $setPegedescription, $setPagelanguage){
	$this->pagetitle = $setPagetitle;
	$this->pagelink = $setPagelink;
	$this->pegedescription = $setPegedescription;
	$this->pagelanguage = $setPagelanguage;
}

function getDataFrom($setSQL){
	$this->sqlresult = mysql_query($setSQL);
}


function rssHead(){
	$this->XMLdumb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom/\">
	<channel>
		<title>".$this->pagetitle."</title>
		<link>".$this->pagelink."</link>
		<description>".$this->pegedescription."</description>
		<language>".$this->pagelanguage."</language>
		<lastBuildDate>".date("r", time())."</lastBuildDate>\n";
}

function rssItems(){
	while($bla = mysql_fetch_assoc($this->sqlresult)){
		$this->XMLdumb .= "		<item>\n";
		$this->XMLdumb .= "			<title>".$bla['title']."</title>\n";
		$this->XMLdumb .= "			<link>http://bestnewssiteever.com/news/".$bla['id']."/</link>\n";
		$this->XMLdumb .= "			<category>".$bla['category']."</category>\n";
		$this->XMLdumb .= "			<pubDate>".date("r",$bla['pubDate'])."</pubDate>\n";
		preg_match_all("/^(?:[^.]*\.){3}/", $bla['content'], $trimedContent);
		$this->XMLdumb .= "			<description>".$trimedContent[0][0]."..</description>\n";
		$this->XMLdumb .= "		</item>\n";
	}
}

function rssFooter(){
	$this->XMLdumb .= " 	</channel>
</rss>";
}

function writeXML(){
	$this->rssHead();
	$this->rssItems();
	$this->rssFooter();
	return $this->XMLdumb;
}

function saveXML($file){
	$fp = fopen($file,"w+");
	flock($fp,2);
	fwrite($fp,$this->writeXML());
	flock($fp,3);
	fclose($fp);
}
}

$Bar = new RSS();
$Bar->getDataFrom("SELECT * FROM news ORDER BY pubDate DESC");
$Bar->setHead("TITLE","http://domain.de","DESCRIPTION","en-EN");
$Bar->saveXML("blub.xml");

?>

Initial URL

                                

Initial Description
this will give you a lovely RSS.

Im sure you will need to edit the database stuff and row names

Initial Title
OOP RSS/XML class for PHP

Initial Tags
php, xml

Initial Language
PHP