View Full Version : Using PHP to generate XML for RSS feed

11-10-2007, 08:53 PM
I'm using PHP to generate a dynamic RSS feed. The PHP code I'm using checks for $_GET variables, and depending on what it finds, runs a certain mysql query, and then generates xml based on the result.

For some reason, the RSS feed only seems to work for some of the $_GET variables. Strangely, this is not because there is any problem with the query. In all cases, the query is successfully executed and the XML code is (from what I can tell) correctly generated. It's just that for some queries, the RSS feed appears blank. However, when viewing the xml source, it looks quite fine.

This problem does not occur in Safari but does affect Firefox.

Here is the PHP code

header( 'Content-Type: text/xml' );
echo "<?xml version='1.0' ?>
<?xml-stylesheet href='rss.xsl' type='text/xsl' media='screen' ?>
<rss version='2.0'>

$dbc= mysql_connect () or die ('Error connecting to database: ' . mysql_error());

$dayago = date('Y-m-d H-i:s', mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
$weekago = date('Y-m-d H-i:s', mktime(0, 0, 0, date('m'), date('d')-7, date('Y')));
$monthago = date('Y-m-d H-i:s', mktime(0, 0, 0, date('m')-1 , date('d'), date('Y')));
$yearago = date('Y-m-d H-i:s', mktime(0, 0, 0, date('m') , date('d'), date('Y')-1));
$all = "2006-01-01 00:00:00";

if (isset($_GET['organize']) && $_GET['organize'] != ""){
$organize = mysql_real_escape_string($_GET['organize']);
$filter = ${$organize.'ago'};
else {
$filter = $all; //default organize

$whereclause = "dateSubmitted > '$filter'";

if (isset($_GET['category']) && $_GET['category'] != ""){
$category = mysql_real_escape_string($_GET['category']);
$whereclause = "$whereclause AND category='$category'";
if (isset($_GET['subcategory']) && $_GET['subcategory'] != ""){
$subcategory = mysql_real_escape_string($_GET['subcategory']);
$whereclause = "$whereclause AND subcategory='$subcategory'";
if (isset($_GET['subsubcategory']) && $_GET['subsubcategory'] != ""){
$subsubcategory = mysql_real_escape_string($_GET['subsubcategory']);
$whereclause = "$whereclause AND subsubcategory='$subsubcategory'";


mysql_select_db ("knowidea_ideas");

$query = "SELECT user, title, idea, overallrating, ideaid, dateSubmitted, category, subcategory, subsubcategory, totalVotes FROM ideas WHERE ($whereclause) ORDER BY dateSubmitted DESC LIMIT 50";
$result = mysql_query($query, $dbc);
while ($r = mysql_fetch_assoc($result)){

$datesub = explode(" ",$r['dateSubmitted']);
$datestamp = explode("-",$datesub[0]);
$dateformatted = date('D, d M Y', mktime(0,0,0,$datestamp[1],$datestamp[2],$datestamp[0]));
$timeformatted = $datesub[1];
if ($r['totalVotes'] == ""){$totalVotes = 0;}else{$totalVotes = $r['totalVotes'];}

$idea = preg_replace('/(\\\")/','"', $r['idea']);
$idea = preg_replace("/(\\\')/","'", $idea);
$title = preg_replace('/(\\\")/','"', $r['title']);
$title = preg_replace("/(\\\')/","'", $title);

$idealen = strlen($idea);
if ($idealen > 500){
$idea = substr($idea, 0, 500).'...';


if (isset($r['subcategory'])){
echo "<subcategory>{$r['subcategory']}</subcategory>";
if (isset($r['subsubcategory'])){
echo "<subsubcategory>{$r['subsubcategory']}</subsubcategory>";

<pubDate>$dateformatted $timeformatted EST</pubDate>

echo "

I can't really figure out what the significant difference is between the pages that do work and the pages that don't. After all, they're all just the same page, feed.php, with different queries run.

This page (http://www.knowidea.org/rss) lists all of the different feeds available. Each link on it just sends you to feed.php and attaches a different $_GET value. Some work and some do not. For instance, Business & Entrepreneurial works, and All Ideas does not.

Any help in this matter would be greatly appreciated I'm hoping that the problem is something simple. It seems like it's very nearly functional.


11-10-2007, 11:41 PM
you need to escape the contents, so it doesn't interfere with the XML, running it through htmlentities() should do it.

http://feedvalidator.org might help you

11-10-2007, 11:46 PM
Ah the beauty of a simple solution. Thank you. It worked like a charm.