How to connect a RSS feed with a database?

Discussion in 'XML & RSS' started by scutari, Aug 18, 2008.

  1. #1
    let us say, we have a table database with the url,publish date and title of an article.

    So can I use php inside xml files without using CDATA so that I generate the xml code from php?

    For example:

    
    
    <?php
    $query = "SELECT * bla bla bla";
    $results = mysql_query($query);
    
    while ($row = mysql_fetch_array($results)) {
    
    echo "<item><br>
    <title>{$row['title']}</title><br>
    <link>{$row['link']}</link<br>
    <guid>{$row['link']}</guid><br>
    <pubDate>{$row['date']}</pubDate><br>
    <description>{$row['subject']}</description><br>
    </item><br>";
    
    }
    
    ?>
    
    
    PHP:
    Could this work?
     
    scutari, Aug 18, 2008 IP
  2. Viktor_Lopuh

    Viktor_Lopuh Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Think no, but it only my idea...
     
    Viktor_Lopuh, Sep 7, 2008 IP
  3. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey Scutari
    Not sure about your echo line with the {}, but that may just be my limited knowledge on php. In short, you sure can create a dynamically updated rss feed using php. Below is an example (bu if your echo method works, your way will work I think):
    
    <?php 
    echo"<?xml version='1.0' encoding='UTF-8'?>"; ?>
    
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    
    <channel>
    <title>Your title</title>
    <description>Your description</description>
    <link>www.yourlink.com/</link>
    <lastBuildDate><?php echo date(r);?></lastBuildDate>
    <pubDate>Fri, 22 Aug 2008 22:47:37 +0200 [just needs to be in this format - change the values to your liking]</pubDate>
    <?php 
    
    $sql=mysql_query("SELECT * blah blah blah");
    
    while($row = mysql_fetch_array($sql))
    {
    
    ?>
    <item>
    <title><?php echo$row['title']; ?></title>
    <description><?php $row['description']; ?></description>
    <link>your link here</link>
    <pubDate><?php 
    //This next php code is just because my DB doesn't store the dates in the correct format.
    $my_date=$row['datetime']; $my_time=strtotime($my_date);echo date("r", $my_time); ?></pubDate>
    </item>
    <?php
    }
    ?>
    
    </channel>
    </rss>
    
    Code (markup):
    Change the mysql_querystring and the $row[]s to your configuration and you should be good to go. Naturally you'll also need to connect to your database ;)
    ***Naturally you'll need to call your file name.php instead of name.xml
     
    lruneh, Sep 8, 2008 IP
  4. Dreads

    Dreads Well-Known Member

    Messages:
    1,884
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    150
    #4
    err lol
    you need to add the part where it connects to the db and disconnects 0o lol
     
    Dreads, Sep 9, 2008 IP
  5. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Which I also wrote.... lol
     
    lruneh, Sep 9, 2008 IP
  6. scutari

    scutari Peon

    Messages:
    431
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hey I have tried to do what you said..but I think that the rss reader will not read the php extension..
     
    scutari, Sep 10, 2008 IP
  7. lruneh

    lruneh Peon

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    That should not pose a problem. The important thing is that the first thing you declare is that this is
    <?xml version='1.0' encoding='UTF-8'?> 
    Code (markup):
    and then that it is
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    Code (markup):
    That will let the rss reader know what kind of document we're talking about.
     
    lruneh, Sep 10, 2008 IP