Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Using PHP to generate XML for RSS feed

    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
    PHP Code:
    <?php 
    header
    'Content-Type: text/xml' );
    echo 
    "<?xml version='1.0' ?>
    <?xml-stylesheet href='rss.xsl' type='text/xsl' media='screen' ?>
    <rss version='2.0'>
    <channel>
    <title>KnowIdea</title>
    <language>en-us</language>
    <link>http://knowidea.org/</link>
    <description>KnowIdea</description>
    "
    ;

    $dbcmysql_connect () or die ('Error connecting to database: ' mysql_error());

    $dayago date('Y-m-d H-i:s'mktime(000date('m'), date('d')-1date('Y')));
    $weekago date('Y-m-d H-i:s'mktime(000date('m'), date('d')-7date('Y')));
    $monthago date('Y-m-d H-i:s'mktime(000date('m')-date('d'), date('Y')));
    $yearago date('Y-m-d H-i:s'mktime(000date('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($idea0500).'...';
        }
        
        echo
    "
        <item>
        <title>$title</title>
        <description>$idea</description>
        <link>http://www.knowidea.org/idea{$r['ideaid']}</link>
        <user>{$r['user']}</user>
        <category>{$r['category']}</category>"
    ;
        
        if (isset(
    $r['subcategory'])){
            echo 
    "<subcategory>{$r['subcategory']}</subcategory>";
            if (isset(
    $r['subsubcategory'])){
                echo 
    "<subsubcategory>{$r['subsubcategory']}</subsubcategory>";
            }
        }
        
        echo
    "
        <rating>{$r['overallrating']}</rating>
        <pubDate>$dateformatted $timeformatted EST</pubDate>
        <votes>$totalVotes</votes>
        </item>
        "
    ;
    }

    echo 
    "
    </channel>
    </rss>
    "
    ;
    ?>
    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 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.

    Thanks

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Maryland, USA
    Posts
    165
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Ah the beauty of a simple solution. Thank you. It worked like a charm.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •